溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

微信小程序scroll-view組件如何實(shí)現(xiàn)滾動(dòng)動(dòng)畫

發(fā)布時(shí)間:2021-07-06 09:34:39 來源:億速云 閱讀:483 作者:小新 欄目:web開發(fā)

這篇文章主要介紹了微信小程序scroll-view組件如何實(shí)現(xiàn)滾動(dòng)動(dòng)畫,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

具體內(nèi)容如下

實(shí)現(xiàn)原理

利用scroll-view的scroll-into-view屬性進(jìn)行定位;
利用scroll-view的scroll-with-animation屬性實(shí)現(xiàn)滾動(dòng)動(dòng)畫過度。

WXML

<view class="right-nav">
  <view bindtap="getCurrentCode" class="{{chooseIndex == index ? '.city-list-active' : ''}}" wx:for="{{cityList}}"  data-code="{{item.code}}" data-index="{{index}}">
  {{item.code}}
  </view>
</view>

<view class="city-layer {{isShowLayer ? '' : 'layer-hide'}}">
 {{codeY}}
</view>

<view class="current-choose-city">當(dāng)前選擇機(jī)場(chǎng):{{chooseCity}}</view>
<scroll-view class="city-scroll" scroll-y="true" scroll-into-view="{{codeY}}" scroll-with-animation="true"  bindscroll="scroll">
  <view class="city-box" wx:for="{{cityList}}" wx:key="{{item.code}}">
    <view class="city-code" id="{{item.code}}">{{item.code}}</view>
    <view class="city-list" wx:for="{{item.cityList}}" wx:for-item="city" bindtap="getChooseCity" data-city="{{city}}"> 
       {{city}} 
    </view> 
  </view>
</scroll-view>

WXSS

.current-choose-city{
 position: fixed;
 width: 100%;
 height: 50px;
 line-height: 50px;
 padding: 0 10px;
 top: 0;
 left: 0;
 background-color: #fff;
 z-index: 10;
}
.right-nav{
 width: 30px;
 color: #888;
 text-align: center;
 position: fixed;
 bottom: 0;
 right: 0;
 background-color: rgb(200, 200, 200);
 z-index: 9;
}
.city-scroll{padding-top: 50px;}
.city-code{
 background-color: #f7f7f7;
}
.city-list,.city-code{
 height: 39px;
 line-height: 40px;
 padding: 0 30px 0 10px;
 overflow: hidden;
 border-bottom: 1px solid #c8c7cc;
}
.city-list-active{color:#007aff;}

/*提示點(diǎn)擊的字母 */
.city-layer{
 width: 70px;
 height: 70px;
 line-height: 70px;
 text-align: center;
 border-radius: 50%;
 color: #fff;
 background-color: rgba(0, 0, 0, .7);
 position: fixed;
 top: calc(50% - 35px);
 left:calc(50% - 35px);
 z-index: 11;
}
.layer-hide{display: none;}

JS

var city_list = require('./city.js');

Page({
 data: {
  cityList: city_list.city,
  chooseCity: '您還未選擇機(jī)場(chǎng)!',
  isShowLayer: false,
  chooseIndex: 0,
  codeY: 'A',
  codeHeight: null,
  cityHeight:null
 },
 onLoad (options) {
  var windowHeight = wx.getSystemInfoSync().windowHeight;
  this.setData({ 
   codeHeight: (windowHeight - 50) / this.data.cityList.length,
   cityHeight: windowHeight - 50,
  });
 },
 getCurrentCode(e){
  var self = this;
  this.setData({ 
   codeY: e.target.dataset.code,
   chooseIndex: e.target.dataset.index,
   isShowLayer: true 
  })
  setTimeout(() => {
   self.setData({ isShowLayer: false })
  },500);
 },
 getChooseCity(e){
  this.setData({ chooseCity: e.target.dataset.city });
 }
})

對(duì)比

微信小程序—-全國(guó)機(jī)場(chǎng)索引列表(MUI索引列表)

對(duì)比結(jié)果總結(jié)

  • 由于scroll-view的scroll-into-view屬性是滾動(dòng)到指定id位置,所以,在列表的字母行加上id屬性;

  • 由于scroll-view的scroll-into-view屬性實(shí)現(xiàn)了滾動(dòng)到指定位置,所以減少了scrollTop的計(jì)算;

  • 由于scroll-view的scroll-with-animation屬性,實(shí)現(xiàn)了滾動(dòng)動(dòng)畫過度效果;

  • 減少了計(jì)算scrollTop的循環(huán)消耗;

  • js代碼量減少,減少this.setData方法的變量設(shè)置。

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“微信小程序scroll-view組件如何實(shí)現(xiàn)滾動(dòng)動(dòng)畫”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI