当前位置:网站首页>微信小程序触底加载与下拉刷新的实现
微信小程序触底加载与下拉刷新的实现
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 () {
},
onloadonReachBottomloader函数思考
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()停止下拉刷新
最后
边栏推荐
- flink cdc不能监听mysql日志,大家遇到过这个问题吧?
- WorkManager的学习二
- A high density 256 channel electrode cap for dry EEG
- Energy momentum: how to achieve carbon neutralization in the power industry?
- 《剑来》语句摘录(七)
- 【 conseils 】 obtenir les valeurs des axes X et y de la fonction cdfplot dans MATLAB
- AtCoder Beginner Contest 258「ABCDEFG」
- Unity粒子特效系列-毒液喷射预制体做好了,unitypackage包直接用 -下
- [C language] the use of dynamic memory development "malloc"
- [system design] index monitoring and alarm system
猜你喜欢

自动化规范检查软件如何发展而来?

A large number of virtual anchors in station B were collectively forced to refund: revenue evaporated, but they still owe station B; Jobs was posthumously awarded the U.S. presidential medal of freedo

. Net delay queue

Timed disappearance pop-up

Usage differences between isempty and isblank

Comment obtenir le temps STW du GC (collecteur d'ordures)?

【小技巧】获取matlab中cdfplot函数的x轴,y轴的数值

The king of pirated Dall · e? 50000 images per day, crowded hugging face server, and openai ordered to change its name

Redis如何实现多可用区?

基于单片机步进电机控制器设计(正转反转指示灯挡位)
随机推荐
Swift saves an array of class objects with userdefaults and nssecurecoding
MySQL digital type learning notes
C语言实现QQ聊天室小项目 [完整源码]
MySQL character type learning notes
The most complete is an I2C summary
> Could not create task ‘:app:MyTest.main()‘. > SourceSet with name ‘main‘ not found.问题修复
Applet image height adaptation and setting text line height
面试:Bitmap像素内存分配在堆内存还是在native中
Unity particle special effects series - the poison spray preform is ready, and the unitypackage package is directly used - on
Uni app running to wechat development tool cannot Preview
Excerpt from "sword comes" (VII)
How do programmers live as they like?
程序员搞开源,读什么书最合适?
Comparison of batch merge between Oracle and MySQL
Matrix processing practice
To bring Euler's innovation to the world, SUSE should be the guide
如何写出高质量的代码?
Swift tableview style (I) system basic
CSDN always jumps to other positions when editing articles_ CSDN sends articles without moving the mouse
能源势动:电力行业的碳中和该如何实现?