查看文檔颓鲜,在用page()函數(shù)注冊(cè)頁面的時(shí)候有這樣的兩個(gè)對(duì)象參數(shù)用戶判斷用戶在最頂部下拉和到達(dá)最底部,
在小程序里票腰,用戶頂部下拉是默認(rèn)禁止的,我們需要把他設(shè)置為啟用女气,在app.json中的設(shè)置對(duì)所有頁面有效杏慰,在單獨(dú)頁面設(shè)置則對(duì)當(dāng)前頁面有效;
看一下json文件
[javascript] view plaincopy
<embed id="ZeroClipboardMovie_1" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_1" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=1&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">
<embed id="ZeroClipboardMovie_4" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_4" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=4&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">
- "enablePullDownRefresh": true,
注意這里的true是布爾型而不是字符;
有同學(xué)說設(shè)置完之后可以下拉缘滥,但是看不到圖標(biāo)轰胁;
在app.json中這樣設(shè)置;
這樣下拉之后就可以看到了朝扼;
每個(gè)頁面生成的時(shí)候已經(jīng)默認(rèn)為我們?cè)O(shè)置了前面提到的onPullDownRefresh函數(shù)和onReachBottom函數(shù)
直接上代碼:
page為全局變量赃阀,用在在后面的加載請(qǐng)求,這里要跟后端大哥討論好請(qǐng)求吟税;
[javascript] view plaincopy
<embed id="ZeroClipboardMovie_2" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_2" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=2&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">
<embed id="ZeroClipboardMovie_5" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_5" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=5&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">
- // 下拉刷新
- onPullDownRefresh: function () {
- // 顯示頂部刷新圖標(biāo)
- wx.showNavigationBarLoading();
- var that = this;
- wx.request({
- url: 'https://xxx/?page=0',
- method: "GET",
- header: {
- 'content-type': 'application/text'
- },
- success: function (res) {
- that.setData({
- moment: res.data.data
- });
- // 設(shè)置數(shù)組元素
- that.setData({
- moment: that.data.moment
- });
- console.log(that.data.moment);
- // 隱藏導(dǎo)航欄加載框
- wx.hideNavigationBarLoading();
- // 停止下拉動(dòng)作
- wx.stopPullDownRefresh();
- }
- })
- },
上拉加載更多:
[javascript] view plaincopy
<embed id="ZeroClipboardMovie_3" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_3" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=3&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">
<embed id="ZeroClipboardMovie_6" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_6" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=6&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">
/**
- 頁面上拉觸底事件的處理函數(shù)
*/
onReachBottom: function () {
var that = this;
// 顯示加載圖標(biāo)
wx.showLoading({
title: '玩命加載中',
})
// 頁數(shù)+1
page = page + 1;
wx.request({
url: 'https://xxx/?page=' + page,
method: "GET",
// 請(qǐng)求頭部
header: {
'content-type': 'application/text'
},
success: function (res) {
// 回調(diào)函數(shù)
var moment_list = that.data.moment;
for (var i = 0; i < res.data.data.length; i++) {
moment_list.push(res.data.data[i]);
}
// 設(shè)置數(shù)據(jù)
that.setData({
moment: that.data.moment
})
// 隱藏加載框
wx.hideLoading();
}
})
},
完成凹耙。