欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

微信小程序scroll-view點(diǎn)擊項(xiàng)自動居中效果的實(shí)現(xiàn)

 更新時間:2020年03月25日 09:25:01   作者:WahFung  
這篇文章主要介紹了微信小程序scroll-view點(diǎn)擊項(xiàng)自動居中效果的實(shí)現(xiàn)代碼,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

scroll-view | 微信開放文檔

效果

代碼

布局樣式代碼省略,以下只展示邏輯代碼

<scroll-view scroll-x scroll-left="{{scrollLeft}}" scroll-with-animation class="scroll-view">
 <view class="class-item" wx:for="{{classList}}" wx:key="id" bindtap="switchClass">
  <text class="name">{{item.className}}</text>
 </view>
</scroll-view>
onReady(){
 wx.createSelectorQuery().select('.scroll-view').boundingClientRect((rect)=>{
  this.data.scrollViewWidth = Math.round(rect.width)
 }).exec()
},
switchClass(){
 let offsetLeft = e.currentTarget.offsetLeft
 this.setData({
  scrollLeft: offsetLeft - this.data.scrollViewWidth/2
 })
}

我們想要的是居中的效果,所以觸發(fā)滾動的條件是點(diǎn)scroll-view寬度一半之后的項(xiàng)才開始滾動,所以需要減去寬度的一半

offsetLeft為相對于scroll-view總長度的X軸距離,只要相減計(jì)算后的數(shù)值為正數(shù),就可以證明上面的條件,觸發(fā)滾動

推薦閱讀:微信小程序 scroll-view實(shí)現(xiàn)錨點(diǎn)滑動的示例

總結(jié)

到此這篇關(guān)于微信小程序scroll-view點(diǎn)擊項(xiàng)自動居中效果的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)小程序scroll-view自動居中內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論