4_上拉加載下拉刷新
業務代碼
const app = getApp()
-
Page({
data: {
words: [],
// 上拉加載的flag
flagLoadmore: false,
// loading界面顯示
flagLoading: true,
// 下拉刷新的flag
flagPull: false,
// 沒有更多
flagNoMore: false,
// 頁碼 【頁碼,總頁碼,須要在網絡請求中獲取,這裏只是做爲測試使用】
page: 1,
// 總頁碼
totalPage: 1,
-
time: 2000,
},
onLoad: function () {
-
},
onReady: function () {
-
setTimeout(() => {
this.setData({
words: [
{ message: 'haha' }, { message: 'haha' }, { message: 'haha' }, { message: 'haha' },
{ message: 'haha' }, { message: 'haha' }, { message: 'haha' }, { message: 'haha' },
{ message: 'haha' }, { message: 'haha' }, { message: 'haha' }
],
flagLoading: false
})
}, 5000)
},
// 下拉刷新回調接口
onPullDownRefresh: function () {
-
let flagPull = this.data.flagPull
-
// 防止爆破下拉【一會兒拉了好屢次】
if (flagPull) return
-
let words = this.data.words
let time = this.data.time
-
-
setTimeout(() => {
for (let i = 0; i < 11; i++) {
words.unshift({ message: 'world' + i })
}
-
this.setData({
words: words,
flagPull: false
})
wx.hideNavigationBarLoading() //完成中止加載
wx.stopPullDownRefresh() //中止下拉刷新
}, time)
-
this.setData({ flagPull: true })
},
-
onReachBottom: function () {
let flag = this.data.flagLoadmore
let page = this.data.page
let totalPage = this.data.totalPage
let time = this.data.time
-
// 防止重複調用
if (flag == true) return
-
if (page > totalPage) {
this.setData({ flagNoMore: true })
return
}
-
let words = this.data.words
-
setTimeout(() => {
page++
for (let i = 0; i < 11; i++) {
words.push({ message: 'world' + i })
}
-
this.setData({
words: words,
page: page,
flagLoadmore: false
})
}, time)
-
this.setData({ flagLoadmore: true })
},
-
onItemClick: function (e) {
var index = e.currentTarget.dataset.index; //打印能夠看到,此處已獲取到了對應的id
console.log(index);
-
-
wx.navigateTo({
url: '../toast/toast',
})
}
});
界面UI【聯合weui】
1. weui樣式引入
在github中下載weui的包,解壓後,把weui.wxss引入
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
2. 佈局樣式
<!--index.wxml-->
<view class="page">
-
<!-- 上拉加載的提示 -->
<view class="weui-loadmore" wx:if="{{flagLoading}}" style='margin:0 auto; padding:10px'>
<view class="weui-loading"></view>
</view>
-
<view>
<view wx:for="{{words}}" bindtap="onItemClick" data-index="{{index}}" class="loadmore-list" hover-class='weui-grid_active'>
{{index}}: {{item.message}}
</view>
-
<!-- 上拉加載的提示 -->
<view class="weui-loadmore" wx:if="{{flagLoadmore}}" style='margin:0 auto; padding:10px'>
<view class="weui-loading"></view>
<view class="weui-loadmore__tips">正在加載</view>
</view>
-
<view class="weui-loadmore" wx:if="{{flagNoMore}}" style='margin:0 auto; padding:10px'>
<view class="weui-loadmore__tips">暫無數據</view>
</view>
-
</view>
-
</view>
json文件配置
![](http://static.javashuo.com/static/loading.gif)
注意
![](http://static.javashuo.com/static/loading.gif)
歡迎關注本站公眾號,獲取更多信息