首页 > 开发宝典 > 小程序开发

微信小程序上拉触底加载实现-onReachBottom

2020-05-18 17:30:33 ·

小程序拥有页面上拉触底事件的处理函数onReachBottom

当上拉至底部时就会触发onReachBottom函数来进行相应动作。也就是说小程序自带能捕捉到用户触底事件的函数。我们无需再进行捕捉用户事件了!!!

直接上代码

  data: {
    limit: 11,//显示数据量
    list: '',
    page: 1,//当前页
    load: true,
    loading: false,//加载动画的显示
  },  
 
onReachBottom: function () {
    var that = this;
    if (that.data.load) {//全局标志位,方式请求未响应是多次触发
      if (that.data.list.length < that.data.count) {
          that.setData({
            load: false,
            loading: true,//加载动画的显示
          })
          wx.request({
            url: 'url',//请求的地址
            data: {
            },
            method: 'POST',
            success: function (res) {
              console.log(res)
              var content = that.data.list.concat(res.data.data.list)//将放回结果放入content
              that.setData({
                list: content,
                page: that.data.page * 1 + 1,
                load: true,
                loading: false,
               })
            },
            fail: function (res) {
                that.setData({
                  loading: false,
                  load: true,
                })
                wx.showToast({
                  title: '数据异常',
                  icon: 'none',
                  duration: 2000,
                }) 
               },
            complete: function (res) { },
          })
      }
    }
  },


说点什么... 共有条评论