better-scroll上拉加载,下拉刷新

<template>
  <div class="wrapper" ref="wrapper">
    <div>
      <load-more v-show="topLoad" tip="正在刷新"></load-more>
      <slot></slot>
      <load-more v-show="bottomLoad" :tip="loadText"></load-more>
    </div>
  </div>
</template>
<script>
import BScroll from 'better-scroll'
import { LoadMore } from 'vux'
export default {
  name: 'scroll',
  components: {
    LoadMore
  },
  props: {
    loadText: {
      type: String,
      default: ''
    },
    topLoad: {
      type: Boolean,
      default: false
    },
    bottomLoad: {
      type: Boolean,
      default: false
    },
    totalcount: {
      type: String,
      default: ''
    },
    probeType: {
      type: Number,
      default: 1
    },
    click: {
      type: Boolean,
      default: true
    },
    scrollX: {
      type: Boolean,
      default: false
    },
    listenScroll: {
      type: Boolean,
      default: false
    },
    // 用户下拉加载
    pullDownRefresh: {
      type: Boolean,
      default: true
    },
    // 用户上啦刷新
    pullUpLoad: {
      type: Boolean,
      default: false
    },
    listData: {
      type: Array,
      default: () => {
        return []
      }
    }
  },
  mounted () {
    // 保证在DOM渲染完毕后初始化better-scroll
    setTimeout(() => {
      this._initScroll()
    }, 20)
  },
  methods: {
    _initScroll () {
      if (!this.$refs.wrapper) {
        return false
      }
      // better-scroll初始化
      this.scroll = new BScroll(this.$refs.wrapper, {
        probeType: this.probeType,
        click: this.click,
        pullUpLoad: {
          threshold: -50 // 负值是当上拉到超过低部 30px;正值是距离底部距离时
        },
        pullDownRefresh: {
          threshold: 50,
          stop: 20
        }
      })
      // 是否派发滚动到底部事件,用于上拉加载
      if (this.pullUpLoad) {
        this.scroll.on('pullingUp', () => {
          // 滚动到底部
          this.$emit('scrollToEnd')
          this.scroll.finishPullUp()
        })
      }
      // 是否派发顶部下拉事件,用于下拉刷新
      if (this.pullDownRefresh) {
        this.scroll.on('pullingDown', (pos) => {
          // 下拉动作
          this.$emit('pulldown')
          this.scroll.finishPullDown()
        })
      }
    },
    disable () {
      // 代理better-scroll的disable方法
      this.scroll && this.scroll.disable()
    },
    enable () {
      // 代理better-scroll的enable方法
      this.scroll && this.scroll.enable()
    },
    refresh () {
      // console.log('refresh')
      // 代理better-scroll的refresh方法
      this.scroll && this.scroll.refresh()
    },
    scrollTo () {
      // 代理better-scroll的scrollTo方法
      this.scroll && this.scroll.scrollTo.apply(this.scroll, arguments)
    }
  },
  watch: {
    listData () {
      setTimeout(() => {
        this.refresh()
      }, 20)
    },
    bottomLoad () {
      setTimeout(() => {
        this.refresh()
      }, 20)
    }
  }
}
</script>
<style lang="scss" scoped>
.wrapper{
  width: 100%;
  height: 95%;
  position: relative;
  .weui-loadmore{
    width: 100%;
    margin: .1rem 0;
  }
}
</style>

猜你喜欢

转载自www.cnblogs.com/lpdong/p/9211845.html