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


Posted in Javascript onJanuary 31, 2018

本文实例为大家分享了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 });
 }
})

对比

对比结果总结

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

DEMO下载

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

Javascript 相关文章推荐
执行iframe中的javascript方法
Oct 07 Javascript
IE6中使用position导致页面变形的解决方案(js代码)
Jan 09 Javascript
最新28个很棒的jQuery 教程
May 28 Javascript
jQuery创建平滑的页面滚动(顶部或底部)
Feb 26 Javascript
js读取被点击次数的简单实例(从数据库中读取)
Mar 07 Javascript
javascript作用域问题实例分析
Jul 13 Javascript
个人网站留言页面(前端jQuery编写、后台php读写MySQL)
May 03 Javascript
Jquery为DIV添加click事件的简单实例
Jun 02 Javascript
jQuery事件_动力节点Java学院整理
Jul 05 jQuery
vue2.0 实现页面导航提示引导的方法
Mar 13 Javascript
js 实现 list转换成tree的方法示例(数组到树)
Aug 18 Javascript
jquery实现穿梭框功能
Jan 19 jQuery
微信小程序实现全国机场索引列表
Jan 31 #Javascript
微信小程序radio组件使用详解
Jan 31 #Javascript
微信小程序checkbox组件使用详解
Jan 31 #Javascript
原生JS实现多个小球碰撞反弹效果示例
Jan 31 #Javascript
浅析Angular19 自定义表单控件
Jan 31 #Javascript
JavaScript实现计算多边形质心的方法示例
Jan 31 #Javascript
微信小程序switch开关选择器使用详解
Jan 31 #Javascript
You might like
PHP 截取字符串专题集合
2010/08/19 PHP
PHP include_path设置技巧分享
2011/07/03 PHP
PHP开发工具ZendStudio下Xdebug工具使用说明详解
2013/11/11 PHP
php中钩子(hook)的原理与简单应用demo示例
2019/09/03 PHP
在TP5数据库中四个字段实现无限分类的示例
2019/10/18 PHP
Using the TextRange Object
2006/10/14 Javascript
js修改地址栏URL参数解决url参数问题
2012/12/15 Javascript
jquery复选框checkbox实现删除前判断
2014/04/20 Javascript
a标签的href与onclick事件的区别详解
2014/11/12 Javascript
javascript结合canvas实现图片旋转效果
2015/05/03 Javascript
seajs加载jquery时提示$ is not a function该怎么解决
2015/10/23 Javascript
JS中多步骤多分步的StepJump组件实例详解
2016/04/01 Javascript
Bootstrap所支持的表单控件实例详解
2016/05/16 Javascript
jQuery.form.js插件不能解决连接超时(timeout)的原因分析及解决方法
2016/10/14 Javascript
JS笛卡尔积算法与多重数组笛卡尔积实现方法示例
2017/12/01 Javascript
vue组件中的样式属性scoped实例详解
2018/10/30 Javascript
JavaScript中import用法总结
2019/01/20 Javascript
在React中写一个Animation组件为组件进入和离开加上动画/过度效果
2019/06/24 Javascript
JavaScript编写开发动态时钟
2020/07/29 Javascript
js实现鼠标拖曳效果
2020/12/30 Javascript
Django自定义分页效果
2017/06/27 Python
python互斥锁、加锁、同步机制、异步通信知识总结
2018/02/11 Python
TensorFlow实现MLP多层感知机模型
2018/03/09 Python
简单分析python的类变量、实例变量
2019/08/23 Python
Python 最强编辑器详细使用指南(PyCharm )
2019/09/16 Python
python与mysql数据库交互的实现
2020/01/06 Python
Python eval函数介绍及用法
2020/11/09 Python
详解CSS3的perspective属性设置3D变换距离的方法
2016/05/23 HTML / CSS
Jeep牧马人、切诺基和自由人零配件:4 Wheel Drive Hardware
2017/07/02 全球购物
Kate Spade美国官网:纽约新兴时尚品牌,以包包闻名于世
2017/11/09 全球购物
幼儿园教师师德师风演讲稿:爱我所爱 无悔青春
2014/09/10 职场文书
我与祖国共奋进演讲稿
2014/09/13 职场文书
企业整改报告范文
2014/11/08 职场文书
2015年监理个人工作总结
2015/05/23 职场文书
nginx对http请求处理的各个阶段详析
2021/03/31 Servers
MySQL实战记录之如何快速定位慢SQL
2022/03/23 MySQL