1. 程式人生 > >微信小程式開發之下拉重新整理 上拉載入

微信小程式開發之下拉重新整理 上拉載入

微信小程式中的下拉重新整理,上拉載入的功能很常見,目前我知道的有兩種可行的方法,一是scroll-view,二是整個頁面重新整理.今天說說第一種,自己造輪子,難免有些瑕疵,日後慢慢完善.

上gif:


原理: scroll-view中有監聽滑動的方法,這個跟android類似.其中用到了滑動到頂部,滑動到底部的方法.

1.下拉重新整理,在滑動到頂部時,bindscrolltoupper被呼叫,根據自己的業務邏輯請求即可.我的demo只是隨機換了個關鍵字.

2.上拉載入,在滑動到底部時,bindscrolltolower被呼叫,我這裡是頁數加一,根據自己的業務邏輯修改,然後將獲取到的集合新增到scroll-view的資料集合裡即可.


上程式碼:

1.index.js

//index.js
//獲取應用例項
var app = getApp()
Page({
  data: {
    words: [],
    windowHeight: 0,//獲取螢幕高度
    refreshHeight: 0,//獲取高度
    refreshing: false,//是否在重新整理中
    refreshAnimation: {}, //載入更多旋轉動畫資料
    clientY: 0,//觸控時Y軸座標
  },
  onLoad: function () {
    var _this = this;
    //獲取螢幕高度
    wx.getSystemInfo({
      success: function (res) {
        _this.setData({
          windowHeight: res.windowHeight
        })
        console.log("螢幕高度: " + res.windowHeight)
      }
    })
    //獲取words
    wx.request({
      url: 'http://api.avatardata.cn/ChengYu/Search?key=77f072d28eb141c8b6dda145ca364b92&keyWord=好',
      complete: function (res) {
        if (res.data.reason == 'Succes') {
          _this.setData({
            words: res.data.result
          })
        }
      }
    })
  },
  scroll: function () {
    console.log("滑動了...")
  },
  lower: function () {
    var start = 0;
    start += 1;
    console.log("載入了...")
    var _this = this;
    wx.request({
      url: 'http://api.avatardata.cn/ChengYu/Search',
      data: {
        key: '77f072d28eb141c8b6dda145ca364b92', keyWord: '好', page: start
      },
      complete: function (res) {
        if (res.data.reason == 'Succes') {
          var words = _this.data.words.concat(res.data.result);
          _this.setData({
            words: words
          })
        }
      }
    })
  },
  upper: function () {
    console.log("下拉了....")
    //獲取使用者Y軸下拉的位移

    if (this.data.refreshing) return;
    this.setData({ refreshing: true });
    updateRefreshIcon.call(this);
    var _this = this;
    var i = Math.random() //獲得0-1的隨機數
    i = Math.ceil(i * 10) //乘以10並向上去整
    var words = ['龍', '一', '萬', '千', '浩', '金', '得', '而', '可', '人'];
    var word = words[i];
    wx.request({
      url: 'http://api.avatardata.cn/ChengYu/Search?key=77f072d28eb141c8b6dda145ca364b92&keyWord=' + word,

      complete: function (res) {
        if (res.data.reason == 'Succes') {
          setTimeout(function () {
            _this.setData({
              words: res.data.result
            })
          }, 2000)
        }
        setTimeout(function () {
          _this.setData({
            refreshing: false
          })
        }, 2500)
      }
    })
  },
  start: function (e) {
    var startPoint = e.touches[0]
    var clientY = startPoint.clientY;
    this.setData({
      clientY: clientY,
      refreshHeight: 0
    })
  },
  end: function (e) {
    var endPoint = e.changedTouches[0]
    var y = (endPoint.clientY - this.data.clientY) * 0.6;
    if (y > 50) {
      y = 50;
    }
    this.setData({
      refreshHeight: y
    })
  },
  move: function (e) {
    console.log("下拉滑動了...")
  }
})

/**
 * 旋轉上拉載入圖示
 */
function updateRefreshIcon() {
  var deg = 0;
  var _this = this;
  console.log('旋轉開始了.....')
  var animation = wx.createAnimation({
    duration: 1000
  });

  var timer = setInterval(function () {
    if (!_this.data.refreshing)
      clearInterval(timer);
    animation.rotateZ(deg).step();//在Z軸旋轉一個deg角度
    deg += 360;
    _this.setData({
      refreshAnimation: animation.export()
    })
  }, 1000);
}
2.index.wxml
<!--index.wxml-->
  <view class="refresh-block" style="height: {{refreshHeight}}px;" wx:if="{{refreshing}}">
    <image animation="{{refreshAnimation}}" src="../images/refresh.png"></image>
  </view>
<scroll-view scroll-y="true" style="height: {{windowHeight}}px;" bindscroll="scroll" bindscrolltolower="lower" bindscrolltoupper="upper"
catchtouchmove="move" catchtouchstart="start" catchtouchend="end"
>
<block wx:for="{{words}}">
        <view class="item-style">{{item.name}}</view>
</block>
</scroll-view>
3.index.wxss
/**index.wxss**/
.item-style{
  padding: 30rpx;
  font-size: 40rpx;
  text-align: center;
  border-top: 2rpx solid #eee;
}
.refresh-block {
  padding: 15px;
  text-align: center
}
.refresh-block image {
  width: 30px;
  height: 30px;
}

demo下載地址

我的部落格:http://blog.csdn.net/qq_31383345