小程序开发|小程序制作|小程序开发网

搜索

【小程序开发教程】:城市切换

2022-6-25 17:15| 发布者: 心影大都| 查看: 220| 评论: 8

摘要: 在很多微信小程序中因为分区域服务可能都需要城市切换,如果又一个现成的轮子那大家就不用自己造了。 JS文件: var city = require('../../utils/city.js');  Page({  data: {    sea


在很多微信小程序中因为分区域服务可能都需要城市切换,如果又一个现成的轮子那大家就不用自己造了。
JS文件:
  1. var city = require('../../utils/city.js');
  2.   
  3. Page({
  4.   data: {
  5.     searchLetter: [],
  6.     showLetter: "",
  7.     winHeight: 0,
  8.     tHeight:0,
  9.     bHeight:0,
  10.     startPageY:0,
  11.     cityList:[],
  12.     isShowLetter:false,
  13.     scrollTop:0,
  14.     city:""
  15.   },
  16.   onLoad: function (options) {
  17.     // 生命周期函数--监听页面加载
  18.     var searchLetter = city.searchLetter;
  19.     var cityList=city.cityList();
  20.     // console.log(cityInfo);
  21.   
  22.     var sysInfo = wx.getSystemInfoSync();
  23.     console.log(sysInfo);
  24.     var winHeight = sysInfo.windowHeight;
  25.   
  26.     //添加要匹配的字母范围值
  27.     //1、更加屏幕高度设置子元素的高度
  28.     var itemH = winHeight / searchLetter.length;
  29.     var tempObj = [];
  30.     for (var i = 0; i < searchLetter.length; i++) { var temp = {}; temp.name = searchLetter[i]; temp.tHeight = i * itemH; temp.bHeight = (i + 1) * itemH; tempObj.push(temp) } this.setData({ winHeight: winHeight, itemH: itemH, searchLetter: tempObj, cityList:cityList }) console.log(this.data.cityInfo); }, onReady: function () { // 生命周期函数--监听页面初次渲染完成 }, onShow: function () { // 生命周期函数--监听页面显示 }, onHide: function () { // 生命周期函数--监听页面隐藏 }, onUnload: function () { // 生命周期函数--监听页面卸载 }, onPullDownRefresh: function () { // 页面相关事件处理函数--监听用户下拉动作 }, onReachBottom: function () { // 页面上拉触底事件的处理函数 }, onShareAppMessage: function () { // 用户点击右上角分享 return { title: 'title', // 分享标题 desc: 'desc', // 分享描述 path: 'path' // 分享路径 } }, searchStart: function (e) { var showLetter = e.currentTarget.dataset.letter; var pageY = e.touches[0].pageY; this.setScrollTop(this,showLetter); this.nowLetter(pageY,this); this.setData({ showLetter: showLetter, startPageY: pageY, isShowLetter:true, }) }, searchMove: function (e) { var pageY = e.touches[0].pageY; var startPageY=this.data.startPageY; var tHeight=this.data.tHeight; var bHeight=this.data.bHeight; var showLetter = 0; console.log(pageY); if(startPageY-pageY>0){ //向上移动
  31.         if(pageYbHeight){
  32.             // showLetter=this.mateLetter(pageY,this);
  33.             this.nowLetter(pageY,this);
  34.         }
  35.     }
  36.   },
  37.   searchEnd: function (e) {
  38.     // console.log(e);
  39.     // var showLetter=e.currentTarget.dataset.letter;
  40.     var that=this;
  41.     setTimeout(function(){
  42.       that.setData({
  43.       isShowLetter:false
  44.     })
  45.     },1000)
  46.       
  47.   },
  48.   nowLetter: function (pageY, that) {//当前选中的信息
  49.     var letterData = this.data.searchLetter;
  50.     var bHeight = 0;
  51.     var tHeight = 0;
  52.     var showLetter="";
  53.     for (var i = 0; i < letterData.length; i++) { if (letterData[i].tHeight <= pageY && pageY<= letterData[i].bHeight) { bHeight = letterData[i].bHeight; tHeight = letterData[i].tHeight; showLetter = letterData[i].name; break; } } this.setScrollTop(that,showLetter); that.setData({ bHeight:bHeight, tHeight:tHeight, showLetter:showLetter, startPageY:pageY }) }, bindScroll:function(e){ console.log(e.detail) }, setScrollTop:function(that,showLetter){ var scrollTop=0; var cityList=that.data.cityList; var cityCount=0; var initialCount=0; for(var i=0;i

WXML文件:
  1. {{item.name}}{{showLetter}}当前选择城市:{{city}}{{item.initial}}{{ct.city}}
CSS文件:
  1. .searchLetter{
  2.     position: fixed;
  3.     right: 0;
  4.     width: 40px;
  5.     height: 100%;
  6.     text-align: center;
  7.     justify-content: center;
  8.     display: flex;
  9.     flex-direction: column;
  10.     color: #666;
  11.     z-index: 1
  12. }
  13. .searchLetter view{
  14.     height: 70rpx;
  15. }
  16. .touchClass{
  17.     background-color: rgba(0, 0, 0,0.5);
  18.     color: #fff;
  19. }
  20. .showSlectedLetter{
  21.     background-color: rgba(0, 0, 0,0.5);
  22.     color: #fff;
  23.     display: flex;
  24.     justify-content: center;
  25.     align-items: center;
  26.     position: fixed;
  27.     top:50%;
  28.     left: 50%;
  29.     margin: -50px;
  30.     width: 100px;
  31.     height: 100px;
  32.     border-radius:10px;
  33.     font-size: 26px;
  34.     z-index: 1
  35. }
  36. .selection{
  37.     display: flex;
  38.     width: 100%;
  39.     flex-direction: column;
  40. }
  41. .item_letter{
  42.     display: flex;
  43.     background-color: #f8f8f8;
  44.     height: 30px;
  45.     padding-left: 10px;
  46.     align-items: center;
  47. }
  48. .item_city{
  49.     display: flex;
  50.     background-color: #fff;
  51.     height: 40px;
  52.     padding-left: 10px;
  53.     align-items: center;
  54.     border-bottom: 1px solid #f8f8f8
  55. }


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

鲜花

握手

雷人

路过

鸡蛋
发表评论

最新评论

引用 心影大都 2022-6-25 18:21
这个代码实用
引用 附原 2022-6-25 18:11
这个代码实用
引用 心影大都 2022-6-25 18:02
这个代码实用
引用 yangyuan 2022-6-25 18:00
好 学习学习
引用 孙老大 2022-6-25 17:47
感谢楼主无私奉献分享
引用 梅西 2022-6-25 17:40
谢谢  大神
引用 附原 2022-6-25 17:31
学习了,感谢
引用 心影大都 2022-6-25 17:27
学习学习

查看全部评论(8)

返回顶部