赞
踩
上拉触底是移动端的专有名词,通过手指在屏幕上的上拉滑动操作,从而加载更多数据的行为。
在页面的.js文件中,通过onReachBottom()函数即可监听当前页面的上拉触底事件。
上拉触底距离指的是触发上拉触底事件时,滚动条距离页面底部的距离。
可以在全局或页面的 .json 配置文件中,通过 onReachBottomDistance 属性来配置上拉触底的距离。
小程序默认的触底距离是 50px,在实际开发中,可以根据自己的需求修改这个默认值。
getColors() {
wx.request({
url: 'https://applet-base-api-t.itheima.net/api/color',
method: 'GET',
success: ( {data:res})=>{
// console.log(res.data);
this.setData({
colorList: this.data.colorList.concat(res.data)
})
console.log(this.data.colorList);
},
});
},
onLoad(options) {
this.getColors();
},
<view wx:for="{{colorList}}" wx:key="index" class="num-item" style="background-color: rgba({{item}});">{{item}}</view>
/* pages/connect/connect.wxss */
.num-item {
border: 1rpx solid #efefef;
border-radius: 8rpx;
line-height: 200rpx;
margin: 15rpx;
text-align: center;
text-shadow: 0rpx 0rpx 5rpx #fff;
box-shadow: 1rpx 1rpx 6rpx #aaa;
}
onReachBottom() {
this.getColors();
},
getColors() { // 需要展示loading效果 wx.showLoading({ title: '正在加载中...', }); // 展示loading效果 wx.request({ url: 'https://applet-base-api-t.itheima.net/api/color', method: 'GET', success: ( {data:res})=>{ console.log(res.data); this.setData({ colorList: this.data.colorList.concat(res.data) }) console.log(this.data.colorList); }, complete: ()=>{ wx.hideLoading(); } }); },
data: { colorList: [], isLoading: false }, getColors() { this.setData({ isLoading: true }); // 需要展示loading效果 wx.showLoading({ title: '正在加载中...', }); // 展示loading效果 wx.request({ url: 'https://applet-base-api-t.itheima.net/api/color', method: 'GET', success: ( {data:res})=>{ console.log(res.data); this.setData({ colorList: this.data.colorList.concat(res.data) }) console.log(this.data.colorList); }, complete: ()=>{ wx.hideLoading(); this.setData({ isLoading: false }) } }); }, onReachBottom() { if (this.data.isLoading===true) { return; } this.getColors(); },
// pages/connect/connect.js Page({ /** * 页面的初始数据 */ data: { colorList: [], isLoading: false }, getColors() { this.setData({ isLoading: true }); // 需要展示loading效果 wx.showLoading({ title: '正在加载中...', }); // 展示loading效果 wx.request({ url: 'https://applet-base-api-t.itheima.net/api/color', method: 'GET', success: ( {data:res})=>{ console.log(res.data); this.setData({ colorList: this.data.colorList.concat(res.data) }) console.log(this.data.colorList); }, complete: ()=>{ wx.hideLoading(); this.setData({ isLoading: false }) } }); }, /** * 生命周期函数--监听页面加载 */ onLoad(options) { this.getColors(); }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() { }, /** * 生命周期函数--监听页面显示 */ onShow() { }, /** * 生命周期函数--监听页面隐藏 */ onHide() { }, /** * 生命周期函数--监听页面卸载 */ onUnload() { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh() { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom() { if (this.data.isLoading===true) { return; } this.getColors(); }, /** * 用户点击右上角分享 */ onShareAppMessage() { } })
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。