需求:当我们浏览到屏幕末尾时,加载出下一页的内容。
小程序scroll-view文档:https://developers.weixin.qq.com/miniprogram/dev/component/scroll-view.html
注意实现细节:
1、 由于采用的是scroll-view 来实现,因此需要竖向滚动,那么scroll-y 属性的值就必须要设置成 true ,而且scroll-view 要能竖向滚动,必须要设置 高度(height).
2、当我们滚动到屏幕的底部时,会触发bindscrolltolower 事件,lower-threshold 这个属性用于控制距离屏幕底部还剩下多少像素时就开始触发这个事件。
3、当滚动到屏幕顶部时会触发 bindscrolltoupper 事件
4、由于bindscrolltolower 在一瞬间可能触发多次,可能会导致发送多次请求来加载数据,我们必须要保证只能有一个请求去发送数据,因此需要用一个变量进行控制,详情见 js 代码中的 scrollToLower 方法。
wxml
<!--index.wxml-->
<scroll-view class='scroll-view-container' scroll-y='{{true}}' bindscrolltolower='scrollToLower'
bindscrolltoupper='scrollToUpper' lower-threshold='30' upper-threshold='0'>
<block wx:for='{{articles}}' wx:key='{{item.id}}'>
<view class='articles-container'>
<!-- 列表 -->
<view class='info'>
<image class='avatar' src='{{item.imgsrc}}'></image>
<text class='created-at'>{{item.name}}</text>
</view>
</view>
</block>
<view class='data-loading' hidden='{{hidden}}'>
数据加载中...
</view>
</scroll-view>
wxss
.scroll-view-container {
background-color: #fff;
height:100vh;
}
.data-loading {
/* height: 100rpx; */
/* line-height: 100rpx; */
/* background-color: #eee; */
text-align: center;
font-size: 14px;
}
.articles-container {
border-bottom: 1px solid #eee;
margin: 30rpx 10rpx;
background-color: #eee;
}
.articles-container .info {
font-size: 12px;
margin: 20rpx 0rpx;
height: 100%;
display: inline-block;
}
.articles-container .info .avatar {
width: 60rpx;
height: 60rpx;
margin-right: 10rpx;
border-radius: 60rpx;
display: inline-block;
vertical-align: middle;
}
.articles-container .info .created-at {
margin: 0px 20rpx;
display: inline-block;
vertical-align: middle;
}
js
Page({
data: {
/**
* 用于控制当 scroll-view 滚动到底部时,显示 “数据加载中...” 的提示
*/
hidden: true,
/**
* 用于显示文章的数组
*/
articles: [
{
imgsrc:'https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=3139953554,3011511497&fm=26&gp=0.jpg',
name:'爱新觉罗.李四四1'
},
{
imgsrc:'https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=3139953554,3011511497&fm=26&gp=0.jpg',
name:'爱新觉罗.李四四2'
},
{
imgsrc:'https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=3139953554,3011511497&fm=26&gp=0.jpg',
name:'爱新觉罗.李四四3'
},
],
/**
* 数据是否正在加载中,避免用户瞬间多次下滑到底部,发生多次数据加载事件
*/
loadingData: false
},
onLoad: function(options) {
this.loadData(true);
},
loadData: function(tail, callback) {
var that = this;
wx.request({
url: 'xxx',
success: function(r) {
var oldArticles = that.data.articles,
newArticles = tail ? oldArticles.concat(r.data.articles) : r.data.articles.concat(oldArticles);
// tail为true尾部合并,false头部合并
that.setData({
articles: newArticles
});
//执行所传方法
if (callback) {
callback();
}
},
error: function(r) {
console.info('error', r);
},
complete: function() {}
})
},
/**
* 上滑加载更多
*/
scrollToLower: function(e) {
console.log('上滑加载更多')
console.info('scrollToLower', e);
var hidden = this.data.hidden,
loadingData = this.data.loadingData,
that = this;
if (hidden) {
this.setData({
hidden: false
});
console.info(this.data.hidden);
}
if (loadingData) {
return;
}
this.setData({
loadingData: true
});
// 加载数据,模拟耗时操作
wx.showLoading({
title: '数据加载中...',
});
//执行接口方法
setTimeout(function() {
that.loadData(true, () => {
that.setData({
hidden: true,
loadingData: false
});
wx.hideLoading();
});
console.info('上拉数据加载完成.');
}, 1000);
},
scrollToUpper: function(e) {
wx.showToast({
title: '触顶了...',
})
}
})
效果图