当前位置:网站首页>微信小程序触底加载与下拉刷新的实现
微信小程序触底加载与下拉刷新的实现
2022-07-05 10:01:00 【InfoQ】
触底加载
- 封装一个loader函数
- 在监听页面加载的时候触发这个loader函数
- 在监听到触底的时候再次触发这个函数
onLoad: function (options) {
this.loadBooks(this.data.books.length)
},
onReachBottom: function () {
this.loadBooks(this.data.books.length)
},
loadBooks: function () {
},
onload
onReachBottom
loader函数思考
skip(n)
loader函数实现
- 调用wx云开发的api,
skip(n)
从集合的第n项开始获取limit(n)
获取至多n个元素。
- 通过get获取相应数据,这里为n项后6个数据。
- 通过
cancat()
实现数组的合并需要注意的是这里的limit(n)
直接使用是有20的长度限制,如果有刚需,需要获取20个以上的数据集合,那么我们可以在云函数中去解决。
loadBooks: function (n) {
wx.cloud.database().collection('books').skip(n).limit(6).get().then(res => {
console.log(res.data)
this.setData({
books: this.data.books.concat(res.data)
})
})
},
触底加载动画
<view class="loader" wx:if="{{isLoader}}">
<view class="dot"></view>
<view class="dot"></view>
<view class="dot"></view>
<view class="dot"></view>
<view class="dot"></view>
</view>
.dot {
width: 24rpx;
height: 24rpx;
background: #3ac;
border-radius: 100%;
display: inline-block;
animation: slide 1s infinite;
}
.dot:nth-child(n) {
animation-delay: 0.1s;
background: #6cd793;
}
@keyframes slide {
0% {
transform: scale(1);
}
50% {
opacity: .3;
transform: scale(2);
}
100% {
transform: scale(1);
}
}
触底加载的优点
下拉刷新
- 在相应页面的同级json文件中配置一下
enablePullDownRefresh
使其为true
,这里是开启对应页面的下拉刷新设置
{
"usingComponents": {},
"enablePullDownRefresh": true,
}
- 使用
onPullDownRefresh
监听用户下拉动作
onPullDownRefresh: function () {},
- 调用wx的加载api
onPullDownRefresh: function () {
console.log(1)
//在标题栏中显示加载
wx.showNavigationBarLoading()
wx.showLoading({
title: '刷新中...',
})
wx.cloud.database().collection('books').skip(0).limit(6).get().then(res => {
this.setData({
books: res.data
})
wx.hideLoading();
//隐藏导航条加载动画
wx.hideNavigationBarLoading();
//停止下拉刷新
wx.stopPullDownRefresh();
})
},
wx.showNavigationBarLoading()
在导航条进行显示加载动画
wx.hideNavigationBarLoading()
隐藏导航条加载动画
wx.stopPullDownRefresh()
停止下拉刷新
最后
边栏推荐
- ConstraintLayout官方提供圆角ImageFilterView
- Design and Simulation of fuzzy PID control system for liquid level of double tank (matlab/simulink)
- How to get the STW (pause) time of GC (garbage collector)?
- Apple 5g chip research and development failure? It's too early to get rid of Qualcomm
- 一种用于干式脑电图的高密度256通道电极帽
- 字节跳动面试官:一张图片占据的内存大小是如何计算
- [system design] index monitoring and alarm system
- 面试:List 如何根据对象的属性去重?
- Uni app running to wechat development tool cannot Preview
- La vue latérale du cycle affiche cinq demi - écrans en dessous de cinq distributions moyennes
猜你喜欢
How did automated specification inspection software develop?
Kotlin compose multiple item scrolling
How to get the STW (pause) time of GC (garbage collector)?
Design of stepping motor controller based on single chip microcomputer (forward rotation and reverse rotation indicator gear)
《微信小程序-基础篇》小程序中的事件与冒泡
高级 OpenCV:BGR 像素强度图
《天天数学》连载58:二月二十七日
RMS TO EAP通过MQTT简单实现
Constrained layout flow
Mysql80 service does not start
随机推荐
历史上的今天:第一本电子书问世;磁条卡的发明者出生;掌上电脑先驱诞生...
Livedata interview question bank and answers -- 7 consecutive questions in livedata interview~
如何獲取GC(垃圾回收器)的STW(暫停)時間?
> Could not create task ‘:app:MyTest.main()‘. > SourceSet with name ‘main‘ not found.问题修复
SLAM 01.人类识别环境&路径的模型建立
Cent7 Oracle database installation error
Swift tableview style (I) system basic
Activity jump encapsulation
【小技巧】获取matlab中cdfplot函数的x轴,y轴的数值
uniapp + uniCloud+unipay 实现微信小程序支付功能
各位大佬,我测试起了3条线程同时往3个mysql表中写入,每条线程分别写入100000条数据,用了f
程序员如何活成自己喜欢的模样?
Apple 5g chip research and development failure? It's too early to get rid of Qualcomm
Events and bubbles in the applet of "wechat applet - Basics"
StaticLayout的使用详解
把欧拉的创新带向世界 SUSE 要做那个引路人
> Could not create task ‘:app:MyTest. main()‘. > SourceSet with name ‘main‘ not found. Problem repair
QT VT100 parser
小程序中自定义行内左滑按钮,类似于qq和wx消息界面那种
Wechat applet - simple diet recommendation (4)