欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

微信小程序scroll-view组件实现滚动动画

程序员文章站 2022-07-04 23:42:49
本文实例为大家分享了scroll-view组件实现索引列表滚动动画效果,供大家参考,具体内容如下 效果图 实现原理 利用scroll-view的scroll-...

本文实例为大家分享了scroll-view组件实现索引列表滚动动画效果,供大家参考,具体内容如下

效果图

实现原理

利用scroll-view的scroll-into-view属性进行定位;
利用scroll-view的scroll-with-animation属性实现滚动动画过度。

wxml

<view class="right-nav">
  <view bindtap="getcurrentcode" class="{{chooseindex == index ? '.city-list-active' : ''}}" wx:for="{{citylist}}" style="height:{{codeheight}}px" 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">当前选择机场:{{choosecity}}</view>
<scroll-view class="city-scroll" scroll-y="true" scroll-into-view="{{codey}}" scroll-with-animation="true" style="height:{{cityheight}}px" 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;}

/*提示点击的字母 */
.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: '您还未选择机场!',
  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 });
 }
})

对比

微信小程序—-全国机场索引列表(mui索引列表)

对比结果总结

  • 由于scroll-view的scroll-into-view属性是滚动到指定id位置,所以,在列表的字母行加上id属性;
  • 由于scroll-view的scroll-into-view属性实现了滚动到指定位置,所以减少了scrolltop的计算;
  • 由于scroll-view的scroll-with-animation属性,实现了滚动动画过度效果;
  • 减少了计算scrolltop的循环消耗;
  • js代码量减少,减少this.setdata方法的变量设置。

demo下载

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。