当前位置:网站首页>微信小程序 滑动到顶部
微信小程序 滑动到顶部
2022-06-10 14:44:00 【yechaoa】
效果:
点击上图中右下角的图标滑动到顶部。 其实很简单,但也有一些小细节。
1.确定图标按钮的位置
使用绝对位置使其固定在右下角的位置。
wxml:
<icon type="download" size="45" color="#4caf50" bindtap='scrollTop'/>这里使用官方的下载图标,然后修改了一下颜色。
wxss:
icon[type=download] {
position: fixed;
bottom: 30px;
right: 20px;
transform: rotate(180deg);
}- 为了使其不跟随滑动,修改了
position为fixed。 - 原本箭头是向下的,使用
transform: rotate(180deg)旋转180度向上。
2.绑定事件
bindtap='scrollTop'
scrollTop: function() {
wx.pageScrollTo({
scrollTop: 0,
duration: 300
})
},在事件中,我们使用了官方的API wx.pageScrollTo,两个参数,一个是滑动的位置,一个是执行时长。
到此,滑动到顶部的功能就简单实现了。
3.进阶
为什么会有这一步呢,是因为发现无论滑动的距离是远还是近,执行时长都是300,那能不能优化呢,其实是可以的。
微信page中有一个onPageScroll方法(与onLoad平级),监听用户滑动页面事件。 那我们就可以根据滑动的距离动态设置执行时长了。
把时长定义为变量
data: {
scrollDuration: 500,
},在监听事件中赋值
onPageScroll: function(e) {
console.log(e.scrollTop)
this.setData({
scrollDuration: e.scrollTop / 2
});
}- e.scrollTop即为页面滑动距离
- e.scrollTop / 2为执行时长,也可以设置其他值
- 官方提示:请避免在 onPageScroll 中过于频繁的执行 setData 等引起逻辑层-渲染层通信的操作。
修改滑动方法
scrollTop: function() {
wx.pageScrollTo({
scrollTop: 0,
duration: this.data.scrollDuration
})
},ok,到此就全部完成了。
边栏推荐
- 【离散数学期复习系列】三、集合的概念及运算
- Insight technology was selected into the "Aijian · privacy computing manufacturer panorama report" and was rated as a representative manufacturer of financial solutions
- 【LogoDetection 数据集处理】(2)画出训练集图片的标注框
- 如何实现erp外网连接?
- Insight Technology a été sélectionné dans le rapport panorama des fournisseurs d'analyse de l'amour et d'informatique de la vie privée et a été évalué comme représentant des fournisseurs de solutions
- 2022 the 15th Nanjing International Industrial Automation Exhibition
- Does Fortran have a standard library
- 数据湖(六):Hudi与Flink整合
- What is CAS and ABA in CAS
- KAtex problem - the style of equal sign alignment in CSDN editing
猜你喜欢

Meta公司新探索 | 利用Alluxio数据缓存降低Presto延迟

【離散數學期複習系列】二、一階邏輯(謂詞邏輯)

Flutter listview, column, row learning personal summary 2

Yanrong looks at how to realize the optimal storage solution of data Lake in a hybrid cloud environment

CVPR 2022 | frame by frame motion representation of long video based on sequence contrast learning
![[big guy show] aiops in the eyes of Borui data, choosing the right track and the right people](/img/ea/740b877b5330a42065b5c5df0d9888.jpg)
[big guy show] aiops in the eyes of Borui data, choosing the right track and the right people
![[original] poi 5 X xssf and HSSF use custom font colors](/img/fc/0d983205784f3c3118bf4d2a73f842.png)
[original] poi 5 X xssf and HSSF use custom font colors

How to implement the association between interfaces in JMeter?

How can JMeter parameterization be implemented?

This awesome low code generator is now open source!
随机推荐
[cloud native | kubernetes] in depth RC, RS, daemonset, statefulset (VII)
ScrollView 初始化的时候不在最顶部?
Beijing / Shanghai internal promotion | recruitment of full-time interns in the system and network group of Microsoft Research Asia
【LogoDetection 数据集处理】(4)提取每张图片的logo区域
2022第十四届南京国际人工智能产品展会
Binary tree and Figure 2
Does Fortran have a standard library
2022 Shandong Province safety officer C certificate retraining question bank and online simulation examination
jdbc对数据库增删改查
1
QT transfers the received JSON data (including Chinese) Unicode to utf8
Even some people say that ArrayList is twice as large. Today, I will take you to tear up the ArrayList source code
Euclidean algorithm for finding the greatest common factor
列表、数组和张量之间的相互转化
数据湖(六):Hudi与Flink整合
Meta公司新探索 | 利用Alluxio数据缓存降低Presto延迟
Design tools and skills for beginners to build their own blog
【LogoDetection 数据集处理】(3)将训练集按照类别划分为多个文件夹
JS中的call()方法和apply()方法用法总结
【离散数学期复习系列】五、一些特殊的图