上篇文章有介紹一些小程序的自定義組件語法,這篇文章就很少作贅述,重點介紹組件的實現邏輯。git
先把效果圖貼出來,看看要實現的效果:
github
首先仍是設置佈局,從實現效果看,組件可分紅三個部分:展現城市數據的二級列表、側邊的滑動欄以及中間的提示框。也就是一個scroll-view,一個view佈局以及一個text。最終肯定的wxml佈局文件以下:小程序
<scroll-view class='cityList' scroll-y scroll-into-view='{{currentIndex}}' scroll-top='{{scrollTop}}'> <view wx:for='{{allCities}}'> <view class='letter-class' id="id{{index}}">{{item.letter}}</view> <view class='item-class' wx:for='{{item.cityList}}' wx:for-item='cityItem' bindtap='citySelectEvent' data-city='{{cityItem.name}}' data-letter='{{cityItem.key}}'>{{cityItem.name}}</view> </view> </scroll-view> <view class='citySlide' catchtouchstart='slideStart' catchtouchmove='slideMove' catchtouchend='slideEnd'> <view class='citySlideItem' wx:for='{{allCities}}' data-index='{{index}}'>{{item.letter}}</view> </view> <text class='letterText' hidden='{{isLetterHidden}}' style='top:{{letterTop}}px;left:{{letterLeft}}px'>{{letterText}}</text>
佈局文件有了,咱們就須要考慮該如何實現側邊欄與二級列表的聯動效果了。這裏我利用的是scroll-view的scroll-into-view屬性,這個屬性能讓scroll-view滑動到對應id的view的位置,很符合咱們的需求。數組
這裏咱們爲列表的第一級佈局view設置id,併爲scroll-view設置scroll-into-view屬性xss
<scroll-view class='cityList' scroll-y scroll-into-view='{{currentIndex}}' scroll-top='{{scrollTop}}'> . . . //id不能以數字開頭 <view class='letter-class' id="id{{index}}">{{item.letter}}</view>
而後在.js中的data中初始化currentIndex爲'id0'ide
/** * 組件的初始數據 */ data: { currentIndex: 'id0' }
如今的問題就是如何計算出手指在側邊欄上觸摸的是第幾個letter,而後經過改變currentIndex的值,使scroll-view滑動到指定位置來達到聯動的效果。佈局
下面說下思路性能
首先確認側邊欄的高度,我是以屏幕高度減去80px做爲側邊欄高度,在.wxss文件中經過樣式設置。flex
.citySlide { display: flex; flex-direction: column; width: 60rpx; height: calc(100% - 80px); position: absolute; top: 40px; right: 16rpx; align-items: center; justify-content: center; background-color: #ccc; opacity: 0.6; }
而後在.js中經過把屏幕高度減去80px計算出側邊欄的具體高度。再除以數據源的一級數據數組長度,計算出每一個letter的高度。this
wx.getSystemInfo({ success: function (res) { letterLineHeight = (res.windowHeight - 80) / that.data.allCities.length; that.setData({ letterTop: res.windowHeight / 2 - 30, letterLeft: res.windowWidth / 2 - 30 }); } })
計算出每一個letter的高度後,咱們就能夠在側邊欄的觸摸監聽事件中,經過觸摸的點的座標位置,來計算出當前觸摸的letter的序號index,而後再動態修改currentIndex的值爲('id'+index)。就能夠達到聯動的效果了。
顯示在屏幕中央的提示框的實現則比較簡單,經過一個變量isLetterHidden控制text的顯示與隱藏就能夠輕鬆實現。
slideStart: function (e) { //手指觸摸的y座標值 var touchY = e.touches[0].clientY; //佈局距離屏幕頂端距離 var offsetTop = e.currentTarget.offsetTop; var index = parseInt((touchY - offsetTop) / letterLineHeight); this.setData({ currentIndex: 'id' + index, isLetterHidden: false, letterText: this.data.allCities[index].letter }); }, slideMove: function (e) { var touchY = e.touches[0].clientY; var offsetTop = e.currentTarget.offsetTop; var index = parseInt((touchY - offsetTop) / letterLineHeight); this.setData({ currentIndex: 'id' + index, isLetterHidden: false, letterText: this.data.allCities[index].letter }); }, slideEnd: function (e) { var that = this; wx: setTimeout(function () { that.setData({ isLetterHidden: true }); }, 200); }
這裏有一點要注意,設置側邊欄觸摸事件的時候,要選擇catchtouchxxxx事件,不能使用bindtouchxxxx,由於bind事件不會阻止事件冒泡,這樣手指在側邊欄滑動時,會影響到下方的列表的滑動,而catch事件阻止了事件冒泡,就不會出現滑動影響的問題。
再說下城市的數據源格式要求,要求是一個二維數組,而後子項要有name和key兩個字段,分別表明城市名和類別letter。
項目GitHub地址:https://github.com/RaoMeng/TemplateOfHotel