微信小程序实用组件:城市切换

 

http://www.wxapp-union.com/forum.php?mod=viewthread&tid=1644&extra=page%3D1%26filter%3Dlastpost%26orderby%3Dlastpost%26dateline%3D604800%26typeid%3D6

切换城市.gif (159.27 KB, 下载次数: 22)

下载附件

效果图

2016-12-23 22:59 上传

js:
   
[JavaScript]  纯文本查看  复制代码
?
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
var city = require( '../../utils/city.js' );
 
 
Page({
   data: {
     searchLetter: [],
     showLetter: "" ,
     winHeight: 0,
     tHeight:0,
     bHeight:0,
     startPageY:0,
     cityList:[],
     isShowLetter: false ,
     scrollTop:0,
     city: ""
   },
   onLoad: function (options) {
     // 生命周期函数--监听页面加载
     var searchLetter = city.searchLetter;
     var cityList=city.cityList();
     // console.log(cityInfo);
 
     var sysInfo = wx.getSystemInfoSync();
     console.log(sysInfo);
     var winHeight = sysInfo.windowHeight;
 
     //添加要匹配的字母范围值
     //1、更加屏幕高度设置子元素的高度
     var itemH = winHeight / searchLetter.length;
     var tempObj = [];
     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){ //向上移动
         if (pageY<tHeight){
           // showLetter=this.mateLetter(pageY,this);
           this .nowLetter(pageY, this );
         }
     } else { //向下移动
         if (pageY>bHeight){
             // showLetter=this.mateLetter(pageY,this);
             this .nowLetter(pageY, this );
         }
     }
   },
   searchEnd: function (e) {
     // console.log(e);
     // var showLetter=e.currentTarget.dataset.letter;
     var that= this ;
     setTimeout( function (){
       that.setData({
       isShowLetter: false
     })
     },1000)
     
   },
   nowLetter: function (pageY, that) { //当前选中的信息
     var letterData = this .data.searchLetter;
     var bHeight = 0;
     var tHeight = 0;
     var showLetter= "" ;
     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<cityList.length;i++){
          if (showLetter==cityList[i].initial){
            scrollTop=initialCount*30+cityCount*41;
             break ;
          } else {
             initialCount++;
             cityCount+=cityList[i].cityInfo.length;
          }
       }
 
       that.setData({
         scrollTop:scrollTop
       })
   },
   bindCity: function (e){
     var city=e.currentTarget.dataset.city;
     this .setData({city:city})
   }
})


wxml:
[AppleScript]  纯文本查看  复制代码
?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
< view class = "searchLetter touchClass" >
     < view wx : for = "{{searchLetter}}" style = "height:{{itemH}}px" wx : key = "index" data - letter = "{{item.name}}" catchtouchstart = "searchStart" catchtouchmove = "searchMove" catchtouchend = "searchEnd" > { { item . name } } < / view >
< / view >
 
< block wx : if = "{{isShowLetter}}" >
< view class = "showSlectedLetter" >
     { { showLetter } }
< / view >
< / block >
< view > 当前选择城市: { { city } } < / view >
< scroll - view scroll - y = "true" style = "height:{{winHeight}}px" bindscroll = "bindScroll" scroll - top = "{{scrollTop}}" >
     < view class = "selection" wx : for = "{{cityList}}" wx : key = "{{item.initial}}" >
         < view class = "item_letter" > { { item .initial } } < / view >
         < view class = "item_city" wx : for = "{{item.cityInfo}}" wx : for - item = "ct" wx : key = "{{ct.id}}" data - city = "{{ct.city}}" bindtap = "bindCity" >
               { { ct. city } } 
         < / view >
     < / view >
< / scroll - view >


css:
[CSS]  纯文本查看  复制代码
?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
.searchLetter{
     position : fixed ;
     right : 0 ;
     width : 40px ;
     height : 100% ;
     text-align : center ;
     justify- content : center ;
     display : flex;
     flex- direction : column;
     color : #666 ;
     z-index : 1
}
.searchLetter view{
     height : 70 rpx;
}
.touchClass{
     background-color : rgba( 0 , 0 , 0 , 0.5 );
     color : #fff ;
}
.showSlectedLetter{
     background-color : rgba( 0 , 0 , 0 , 0.5 );
     color : #fff ;
     display : flex;
     justify- content : center ;
     align-items: center ;
     position : fixed ;
     top : 50% ;
     left : 50% ;
     margin : -50px ;
     width : 100px ;
     height : 100px ;
     border-radius: 10px ;
     font-size : 26px ;
     z-index : 1
}
.selection{
     display : flex;
     width : 100% ;
     flex- direction : column;
}
.item_letter{
     display : flex;
     background-color : #f8f8f8 ;
     height : 30px ;
     padding-left : 10px ;
     align-items: center ;
}
.item_city{
     display : flex;
     background-color : #fff ;
     height : 40px ;
     padding-left : 10px ;
     align-items: center ;
     border-bottom : 1px solid #f8f8f8
}


文件下载:
本帖隐藏的内容
  switchCity.rar (9.09 KB, 下载次数: 104)
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值