国产成人精品18p,天天干成人网,无码专区狠狠躁天天躁,美女脱精光隐私扒开免费观看

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

發(fā)布時(shí)間:2021-07-06 14:08 來(lái)源:億速云 閱讀:0 作者:小新 欄目: web開(kāi)發(fā)

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

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

實(shí)現效果圖:

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

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

設置js里onPullDownRefresh和onReachBottom方法

下拉加載說(shuō)明:

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

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實(shí)現微信小程序下拉

index.wxml

<!--index.wxml-->
<view class="container" >
 <!--垂直滾動(dòng),這里必須設置高度-->
  <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;


// 請求數據
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必須要設置高度才能監聽(tīng)滾動(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í)的事件,我這里記錄了當前的position.y的值,為了請求數據之后把頁(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");
 }
})

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

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

久久久久成人片免费观看| 日本一道综合久久AⅤ免费| WWW免费视频在线观看播放| 最新国产在线拍揄自揄视频| 最新中文字幕AV无码专区| 亚洲AV最新天堂网址|