小程序使用scroll-view自定义上下刷新加载

2022-03-19  本文已影响0人  清风昙

小程序使用scroll-view自定义上下刷新加载,直接上代码,如下:
index.js

const app = getApp()
Page({
  data: {
    triggered: false, // 设置当前下拉刷新状态,true 表示下拉刷新已经被触发,false 表示下拉刷新未被触发
  },
  onReady: function () {
    setTimeout(() => {
      this.setData({
        triggered: true,
      })
    }, 1000)
  },
  // 自定义下拉刷新控件被下拉
  onPulling(e) {
    console.log('onPulling:', e)
  },
  // 自定义下拉刷新被触发
  onRefresh() {
    console.log('onRefresh')
    if (this._freshing) return
    this._freshing = true
    setTimeout(() => {
      this.setData({
        triggered: false,
      })
      this._freshing = false
    }, 3000)
  },
  // 自定义下拉刷新被复位
  onRestore(e) {
    console.log('onRestore:', e)
  },
  // 自定义下拉刷新被中止
  onAbort(e) {
    console.log('onAbort', e)
  },
  // 滚动到底部
  onLower(e) {
    console.log('onTolower', e)
  }
})

index.wxml

<scroll-view
  scroll-y style="width: 100%; height: 400px;"
  refresher-enabled="{{true}}"
  refresher-threshold="{{100}}"
  refresher-default-style="black"
  refresher-background="ffffff"
  refresher-triggered="{{triggered}}"
  bindrefresherpulling="onPulling"
  bindrefresherrefresh="onRefresh"
  bindrefresherrestore="onRestore"
  bindrefresherabort="onAbort"
  bindscrolltolower="onTolower"
  bindscrolltolower="onLower"
>
  <view wx:for="10" wx:key="index" style="width: 100%;height: 300px;">
    <image  src="https://images.unsplash.com/photo-1565699894576-1710004524ba?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1832&q=80"></image>
  </view>
</scroll-view>
上一篇下一篇

猜你喜欢

热点阅读