当前位置:网站首页>微信小程序触底加载与下拉刷新的实现
微信小程序触底加载与下拉刷新的实现
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()停止下拉刷新
最后
边栏推荐
- 学习笔记6--卫星定位技术(上)
- pytorch输出tensor张量时有省略号的解决方案(将tensor完整输出)
- leetcode:1200. 最小绝对差
- 钉钉、企微、飞书学会赚钱了吗?
- Timed disappearance pop-up
- The king of pirated Dall · e? 50000 images per day, crowded hugging face server, and openai ordered to change its name
- @Serializedname annotation use
- RMS to EAP is simply implemented through mqtt
- Apple 5g chip research and development failure? It's too early to get rid of Qualcomm
- Unity particle special effects series - the poison spray preform is ready, and the unitypackage package is directly used - on
猜你喜欢

Energy momentum: how to achieve carbon neutralization in the power industry?

Learning II of workmanager

What is the most suitable book for programmers to engage in open source?

Workmanager Learning one

《天天数学》连载58:二月二十七日

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

@SerializedName注解使用

Solution of ellipsis when pytorch outputs tensor (output tensor completely)

伪类元素--before和after

ConstraintLayout的流式布局Flow
随机推荐
Unity particle special effects series - the poison spray preform is ready, and the unitypackage package is directly used - on
Events and bubbles in the applet of "wechat applet - Basics"
Timed disappearance pop-up
C语言实现QQ聊天室小项目 [完整源码]
Meitu lost 300 million yuan in currency speculation for half a year. Huawei was exposed to expand its enrollment in Russia. Alphago's peers have made another breakthrough in chess. Today, more big new
Is it really reliable for AI to make complex decisions for enterprises? Participate in the live broadcast, Dr. Stanford to share his choice | qubit · viewpoint
B站大量虚拟主播被集体强制退款:收入蒸发,还倒欠B站;乔布斯被追授美国总统自由勋章;Grafana 9 发布|极客头条...
Constraintlayout officially provides rounded imagefilterview
What is the origin of the domain knowledge network that drives the new idea of manufacturing industry upgrading?
PHP solves the problems of cache avalanche, cache penetration and cache breakdown of redis
MySQL digital type learning notes
Swift uses userdefaults and codable to save an array of class objects or structure instances
[C language] the use of dynamic memory development "malloc"
Have you learned to make money in Dingding, enterprise micro and Feishu?
学习笔记4--高精度地图关键技术(下)
The Alipay in place function can't be found, and the Alipay in place function is offline
天龙八部TLBB系列 - 关于技能冷却和攻击范围数量的问题
How does redis implement multiple zones?
双容水箱液位模糊PID控制系统设计与仿真(Matlab/Simulink)
DDOS攻击原理,被ddos攻击的现象