溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

微信小程序如何實現(xiàn)下拉加載與上拉刷新功能

發(fā)布時間:2021-07-02 14:48:06 來源:億速云 閱讀:116 作者:小新 欄目:web開發(fā)

這篇文章主要介紹微信小程序如何實現(xiàn)下拉加載與上拉刷新功能,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!

微信小程序下拉刷新上拉加載的兩種實現(xiàn)方法

實現(xiàn)效果圖:

微信小程序如何實現(xiàn)下拉加載與上拉刷新功能

方法一:onPullDownRefresh和onReachBottom方法實現(xiàn)小程序下拉加載和上拉刷新

首先要在json文件里設置window屬性

            屬性  類型                          描述
enablePullDownRefreshBoolean是否開啟下拉刷新,詳見頁面相關事件處理函數(shù)。

設置js里onPullDownRefresh和onReachBottom方法

    屬性   類型         描述
onPullDownRefreshfunction頁面相關事件處理函數(shù)——監(jiān)聽用戶下拉動作
onReachBottomfunction頁面上拉觸發(fā)底事件的處理函數(shù)

下拉加載說明:

當處理完數(shù)據(jù)刷新后,wx.stopPullDownRefresh可以停止當前頁面的下拉刷新。

onPullDownRefresh(){
  console.log('--------下拉刷新-------')
  wx.showNavigationBarLoading() //在標題欄中顯示加載
 
  wx.request({
     url: 'https://URL',
     data: {},
     method: 'GET',
     // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
     // header: {}, // 設置請求的 header
     success: function(res){
      // success
     },
     fail: function() {
      // fail
     },
     complete: function() {
      // complete
      wx.hideNavigationBarLoading() //完成停止加載
      wx.stopPullDownRefresh() //停止下拉刷新
     }
  })

方法二:

在scroll-view里設定bindscrolltoupper和bindscrolltolower實現(xiàn)微信小程序下拉

    屬性   類型         描述
bindscrolltoupperEventHandle滾動到頂部/左邊,會觸發(fā) scrolltoupper 事件
bindscrolltolowerEventHandle

滾動到底部/右邊,會觸發(fā) scrolltolower 事件

index.wxml

<!--index.wxml-->
<view class="container" >
 <!--垂直滾動,這里必須設置高度-->
  <scroll-view scroll-top="{{scrollTop}}" scroll-y="true"  
    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;


// 請求數(shù)據(jù)
var loadMore = function(that){
  that.setData({
    hidden:false
  });
  wx.request({
    url:url,
    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必須要設置高度才能監(jiān)聽滾動事件,所以,需要在頁面的onLoad事件中給scroll-view的高度賦值
   var that = this;
   wx.getSystemInfo({
     success:function(res){
       that.setData({
         scrollHeight:res.windowHeight
       });
     }
   });
    loadMore(that);
 },
 //頁面滑動到底部
 bindDownLoad:function(){  
   var that = this;
   loadMore(that);
   console.log("lower");
 },
 scroll:function(event){
  //該方法綁定了頁面滾動時的事件,我這里記錄了當前的position.y的值,為了請求數(shù)據(jù)之后把頁面定位到這里來。
   this.setData({
     scrollTop : event.detail.scrollTop
   });
 },
 topLoad:function(event){
  //  該方法綁定了頁面滑動到頂部的事件,然后做上拉刷新
   page = 0;
   this.setData({
     list : [],
     scrollTop : 0
   });
   loadMore(this);
   console.log("lower");
 }
})

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;
}

以上是“微信小程序如何實現(xiàn)下拉加載與上拉刷新功能”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關知識,歡迎關注億速云行業(yè)資訊頻道!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI