微信小程序scroll-view组件实现滚动动画
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了微信小程序scroll-view组件实现滚动动画,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2940字,纯文字阅读大概需要5分钟。
内容图文
![微信小程序scroll-view组件实现滚动动画](/upload/InfoBanner/zyjiaocheng/339/8454213419524a11b6c67a9092be82e0.jpg)
本文实例为大家分享了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下载
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
内容总结
以上是互联网集市为您收集整理的微信小程序scroll-view组件实现滚动动画全部内容,希望文章能够帮你解决微信小程序scroll-view组件实现滚动动画所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。