小程序scroll-view上拉加载更多,下拉刷新数据

小程序scroll-view上拉加载更多,下拉刷新数据

方法一

<scroll-view id="dialog_list" scroll-y="{{true}}" scroll-with-animation='{{true}}' scroll-top='{{scroll_top}}' scroll-into-view="{{toView}}" scroll-with-animation="true" enable-back-to-top="true" style="height:{{comment_bottom}};padding-top:16vh;" id="scroll-wrap">
  <view class="loading_view" wx:if="{{isLoading}}">
    <text class="subtext">正在载入更多...</text>
  </view>
  <view class="subtext" style="text-align:center;height: 50rpx;" wx:if="{{noMore}}">没有更多了~</view>
  <view id="inner-wrap" bindtouchstart="start_fn" bindtouchend="end_fn" class="singleDialog" wx:for="{{testMessageDetail}}" wx:key="item" wx:for-index="index">
  </view>
</scroll-view>
// start: 触摸开始
  start_fn(e) {
    let self = this;
    let touch_down = e.touches[0].clientY;
    this.data.touch_down = touch_down;
    // 获取 inner-wrap 的高度
    wx.createSelectorQuery().select('#inner-wrap').boundingClientRect(function (rect) {
      self.data.inner_height = rect.height;
    }).exec();

    // 获取 scroll-wrap 的高度和当前的 scrollTop 位置
    wx.createSelectorQuery().select('#scroll-wrap').fields({
      scrollOffset: true,
      size: true
    }, function (rect) {
      self.data.start_scroll = rect.scrollTop;
      self.data.height = rect.height;
    }).exec();
  },
  // start: 触摸结束
  end_fn(e) {
    let current_y = e.changedTouches[0].clientY;
    let that = this;
    var time = '';
    let { start_scroll, inner_height, height, touch_down } = this.data;
    if (current_y > touch_down && current_y - touch_down > 20 && start_scroll == 0) {
      // 下拉刷新 的请求和逻辑处理等
      if (!that.data.isCompleted) {
        that.setData({
          isLoading: true
        })
      }else{
        that.setData({
          noMore:true
        })
      }
    }else if (current_y < touch_down && touch_down - current_y >= 20 && inner_height - height == start_scroll) {
      // 上拉加载 的请求和逻辑处理等
      console.log('上拉加载')
    }
  },

方法二

<scroll-view scroll-y="true" bindscrolltolower="refrashList" class="notice_list" wx:if="{{noticeList.length!=0}}"></scroll-view>
refrashList: function() {
    if ((this.data.noticeTotal - pagePage * 10) > 0) {
      this.setData({
        allReady:false
      })
      pagePage++
      this.getAllNotice(pagePage);
    } else {
      wx.showToast({
        title: '没有更多了~',
        icon:'none'
      })
    }
  },
  getAllNotice: function(pagePage) {
    var userId = wx.getStorageSync('userId');
    var that = this
    wx.request({
      url: this.data.host + '/notice/user/page?userId=' + userId + '&page=' + pagePage + '&row=' + 10,
      method: 'get',
      success: function(e) {
        var noticeList = e.data.result.list;
        var newList = [];
        if (pagePage != 1) {
          newList = that.data.noticeList.concat(noticeList)
        } else {
          newList = noticeList
          noticeIndext = 0
        }
        while (noticeIndext < newList.length) {
          newList[noticeIndext].index = noticeIndext + 1;
          noticeIndext++;
          newList[noticeIndext-1].createUserName = decodeURIComponent(noticeList[noticeIndext-1].createUserName)
          newList[noticeIndext - 1].title = decodeURIComponent(noticeList[noticeIndext - 1].title)
          newList[noticeIndext - 1].content = decodeURIComponent(noticeList[noticeIndext - 1].content)
        }
        that.setData({
          allReady:true,
          loading:false,
          noticeList: newList,
          noticeTotal: e.data.result.total
        })
        console.log(noticeList)
      }
    })
  },
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值