微信小程序下拉加載和上拉刷新兩種實(shí)現(xiàn)方法詳解
方法一:onPullDownRefresh和onReachBottom方法實(shí)現(xiàn)小程序下拉加載和上拉刷新
首先要在json文件里設(shè)置window屬性
設(shè)置js里onPullDownRefresh和onReachBottom方法
下拉加載說(shuō)明:
當(dāng)處理完數(shù)據(jù)刷新后,wx.stopPullDownRefresh可以停止當(dāng)前頁(yè)面的下拉刷新。
onPullDownRefresh(){ console.log('--------下拉刷新-------') wx.showNavigationBarLoading() //在標(biāo)題欄中顯示加載 wx.request({ url: 'https://URL', data: {}, method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT // header: {}, // 設(shè)置請(qǐng)求的 header success: function(res){ // success }, fail: function() { // fail }, complete: function() { // complete wx.hideNavigationBarLoading() //完成停止加載 wx.stopPullDownRefresh() //停止下拉刷新 } })
方法二:在scroll-view里設(shè)定bindscrolltoupper和bindscrolltolower實(shí)現(xiàn)微信小程序下拉
index.wxml
<!--index.wxml--> <view class="container" style="padding:0rpx"> <!--垂直滾動(dòng),這里必須設(shè)置高度--> <scroll-view scroll-top="{{scrollTop}}" scroll-y="true" style="height:{{scrollHeight}}px;" class="list" bindscrolltolower="bindDownLoad" bindscrolltoupper="topLoad" bindscroll="scroll"> <view class="item" wx:for="{{list}}"> <image class="img" src="{{item.pic_url}}"></image> <view class="text"> <text class="title">{{item.name}}</text> <text class="description">{{item.short_description}}</text> </view> </view> </scroll-view> <view class="body-view"> <loading hidden="{{hidden}}" bindchange="loadingChange"> 加載中... </loading> </view> </view>
index.js
var url = "http://www.imooc.com/course/ajaxlist"; var page =0; var page_size = 5; var sort = "last"; var is_easy = 0; var lange_id = 0; var pos_id = 0; var unlearn = 0; // 請(qǐng)求數(shù)據(jù)<br> var loadMore = function(that){ that.setData({ hidden:false }); wx.request({ url:url,<br> data:{ page : page, page_size : page_size, sort : sort, is_easy : is_easy, lange_id : lange_id, pos_id : pos_id, unlearn : unlearn }, success:function(res){ //console.info(that.data.list); var list = that.data.list; for(var i = 0; i < res.data.list.length; i++){ list.push(res.data.list[i]); } that.setData({ list : list }); page ++; that.setData({ hidden:true }); } }); } Page({ data:{ hidden:true, list:[], scrollTop : 0, scrollHeight:0 }, onLoad:function(){ // 這里要注意,微信的scroll-view必須要設(shè)置高度才能監(jiān)聽滾動(dòng)事件,所以,需要在頁(yè)面的onLoad事件中給scroll-view的高度賦值 var that = this; wx.getSystemInfo({ success:function(res){ that.setData({ scrollHeight:res.windowHeight }); } }); loadMore(that); }, //頁(yè)面滑動(dòng)到底部 bindDownLoad:function(){ var that = this; loadMore(that); console.log("lower"); }, scroll:function(event){ //該方法綁定了頁(yè)面滾動(dòng)時(shí)的事件,我這里記錄了當(dāng)前的position.y的值,為了請(qǐng)求數(shù)據(jù)之后把頁(yè)面定位到這里來(lái)。 this.setData({ scrollTop : event.detail.scrollTop }); }, topLoad:function(event){ // 該方法綁定了頁(yè)面滑動(dòng)到頂部的事件,然后做上拉刷新 page = 0; this.setData({ list : [], scrollTop : 0 }); loadMore(this); console.log("lower"); }<br> })
index.wxss
/**index.wxss**/ .userinfo { display: flex; flex-direction: column; align-items: center; } .userinfo-avatar { width: 128rpx; height: 128rpx; margin: 20rpx; border-radius: 50%; } .userinfo-nickname { color: #aaa; } .usermotto { margin-top: 200px; } /**/ scroll-view { width: 100%; } .item { width: 90%; height: 300rpx; margin: 20rpx auto; background: brown; overflow: hidden; } .item .img { width: 430rpx; margin-right: 20rpx; float: left; } .title { font-size: 30rpx; display: block; margin: 30rpx auto; } .description { font-size: 26rpx; line-height: 15rpx; }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
javascript中數(shù)組(Array)對(duì)象和字符串(String)對(duì)象的常用方法總結(jié)
這篇文章主要介紹了javascript中數(shù)組(Array)對(duì)象和字符串(String)對(duì)象的常用方法,結(jié)合實(shí)例形式總結(jié)分析了javascript中關(guān)于數(shù)組和字符串的常用函數(shù)與使用技巧,需要的朋友可以參考下2016-12-12js 數(shù)組的for循環(huán)到底應(yīng)該怎么寫?
說(shuō)實(shí)話,我是個(gè)比較喜歡懷疑權(quán)威的人,但是在有些權(quán)威的問題一直在我面前閃,閃啊閃,我就開始不懷疑他們了,因?yàn)橛?0000個(gè)人說(shuō)這個(gè)東西是對(duì)的,我就會(huì)覺得它的確是對(duì)的吧。2010-05-05JSCode all of Brower 全局屏蔽網(wǎng)頁(yè)右鍵功能 具體實(shí)現(xiàn)
JSCode all of Brower 全局屏蔽網(wǎng)頁(yè)右鍵功能 具體實(shí)現(xiàn),需要的朋友可以參考一下2013-06-06Code Review 方法論與實(shí)踐總結(jié)梳理
這篇文章主要為大家介紹了Code Review 方法論與實(shí)踐總結(jié)梳理詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02ECMAScript 6即將帶給我們新的數(shù)組操作方法前瞻
這篇文章主要介紹了ECMAScript 6即將帶給我們新的數(shù)組操作方法前瞻,需要的朋友可以參考下2015-01-01JavaScript console對(duì)象與控制臺(tái)使用示例詳解
這篇文章主要介紹了JavaScript console對(duì)象與控制臺(tái)的使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2022-10-10