1. 程式人生 > >微信小程式下拉載入和上拉重新整理兩種實現方法

微信小程式下拉載入和上拉重新整理兩種實現方法

方法一:onPullDownRefresh和onReachBottom方法實現小程式下拉載入和上拉重新整理

首先要在json檔案裡設定window屬性

            屬性   型別                           描述
enablePullDownRefresh Boolean 是否開啟下拉重新整理,詳見頁面相關事件處理函式。

設定js裡onPullDownRefresh和onReachBottom方法

    屬性    型別          描述
onPullDownRefresh function 頁面相關事件處理函式——監聽使用者下拉動作
onReachBottom function 頁面上拉觸發底事件的處理函式

下拉載入說明:

當處理完資料重新整理後,wx.stopPullDownRefresh可以停止當前頁面的下拉重新整理。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

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實現微信小程式下拉

    屬性    型別          描述
bindscrolltoupper EventHandle 滾動到頂部/左邊,會觸發 scrolltoupper 事件
bindscrolltolower EventHandle

滾動到底部/右邊,會觸發 scrolltolower 事件

index.wxml

<!--index.wxml-->
<view class="container" style="padding:0rpx">
  <!--垂直滾動,這裡必須設定高度-->
    <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 = "https://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;


// 請求資料
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必須要設定高度才能監聽滾動事件,所以,需要在頁面的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的值,為了請求資料之後把頁面定位到這裡來。
     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;
}

效果圖

 

原文連結:https://www.cnblogs.com/simba-lkj/p/6274232.html