小編給大家分享一下微信小程序列表下拉刷新上拉加載的示例分析,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
效果圖
原理
利用微信小程序的onPullDownRefresh函數(shù)(下拉刷新監(jiān)聽函數(shù))和onReachBottom函數(shù)(上拉加載監(jiān)聽函數(shù))監(jiān)聽頁面的下拉和上拉動態(tài),從而對頁面數(shù)據(jù)進行修改!
頁面配置JSON
enablePullDownRefresh:開啟下拉刷新;
onReachBottomDistance:頁面上拉觸底事件觸發(fā)時距頁面底部距離,單位為px。
{ "enablePullDownRefresh": true, "onReachBottomDistance": 50 }
WXML
Item -- {{item}}
JS
此處用setTimeout模擬請求數(shù)據(jù);
加載數(shù)據(jù)限制三次,調(diào)用wx.showToast顯示沒有更多數(shù)據(jù)。
Page({ data: { dataList: [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20], count : 0 }, onPullDownRefresh(){ var self = this; setTimeout(() => { // 模擬請求數(shù)據(jù),并渲染 var arr = self.data.dataList, max = Math.max(...arr); for (var i = max + 1; i <= max + 3; ++i) { arr.unshift(i); } self.setData({ dataList: arr }); // 數(shù)據(jù)成功后,停止下拉刷新 wx.stopPullDownRefresh(); }, 1000); }, onReachBottom(){ var arr = this.data.dataList, max = Math.max(...arr); if (this.data.count < 3) { for (var i = max + 1; i <= max + 5; ++i) { arr.push(i); } this.setData({ dataList: arr, count: ++this.data.count }); } else { wx.showToast({ title: '沒有更多數(shù)據(jù)了!', image: '../../src/images/noData.png', }) } } })
以上是“微信小程序列表下拉刷新上拉加載的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!