美文网首页
vue应用better-scroll

vue应用better-scroll

作者: O蚂蚁O | 来源:发表于2019-06-28 10:23 被阅读0次

第一步:下载插件

npm install better-scroll --save

第二步:组件开发

<template>
  <div ref="wrapper">
    <slot></slot>
  </div>
</template>

<script>
import BScroll from 'better-scroll'
export default {
  props: {
    scrollX: {
      type: Boolean,
      default: false
    },
    scrollY: {
      type: Boolean,
      default: true
    },
    probeType: {
      type: Number,
      default: 1
    },
    click: {
      type: Boolean,
      default: true
    },
    data: {
      type: Array,
      default: null
    },
    listenScroll: {
      type: Boolean,
      default: false
    },
    pullup: {
      type: Boolean,
      default: false
    },
    bounceTop:{
      type: Boolean,
      default: false
    },
    bounceBottom:{
      type: Boolean,
      default: true
    },
    /**
     * 是否派发顶部下拉的事件,用于下拉刷新
     */
    pulldown: {
      type: Boolean,
      default: true
    },
    beforeScroll: {
      type: Boolean,
      default: false
    },
    refreshDelay: {
      type: Number,
      default: 20
    },
    preventDefaultException: {
      type: Object,
      default: function () {
        return {tagName: /^(INPUT|TEXTAREA|BUTTON|SELECT)$/}
      }
    }
  },
  data () {
    return {
    }
  },
  mounted () {
    setTimeout(() => {
      this._initScroll()
    }, 20)
  },
  methods: {
    _initScroll () {
      if (!this.$refs.wrapper) {
        return
      }
      this.scroll = new BScroll(this.$refs.wrapper, {
        scrollX: this.scrollX,
        scrollY: this.scrollY,
        probeType: this.probeType,
        click: this.click,
        bounce: {
            top: this.bounceTop,
            bottom: this.bounceBottom
        },
        pullDownRefresh: {
          threshold: 30, // 下拉距离超过30px触发pullingDown事件
          stop: 20 // 回弹停留在距离顶部20px的位置
        },
        
        preventDefaultException: this.preventDefaultException
      })
      if (this.listenScroll) {
        let me = this
        this.scroll.on('scroll', (pos) => {
          me.$emit('scroll', pos)
        })
      }
      if (this.pullup) {
        this.scroll.on('scrollEnd', () => {
          if (this.scroll.y <= this.scroll.maxScrollY + 50) {
            this.$emit('scrollToEnd')
          }
        })
      }
      // 是否派发顶部下拉事件,用于下拉刷新
      if (this.pulldown) {
        this.scroll.on('pullingDown', (pos) => {
          // // 下拉动作
          // if (pos.y > 50) {
          //   this.$emit('pulldown')
          //   console.log('上拉')
          // }
          this.scroll.finishPullDown()
          // console.log('上拉')
          this.$emit('pulldown')
        })
      }
      if (this.beforeScroll) {
        this.scroll.on('beforeScrollStart', () => {
          this.$emit('beforeScroll')
        })
      }
    },
    enable () {
      this.scroll && this.scroll.enable()
    },
    disable () {
      this.scroll && this.scroll.disable()
    },
    refresh () {
      this.scroll && this.scroll.refresh()
    },
    scrollTo () {
      this.scroll && this.scroll.scrollTo.apply(this.scroll, arguments)
    },
    scrollToElement () {
      this.scroll && this.scroll.scrollToElement.apply(this.scroll, arguments)
    }
  },
  watch: {
    data () {
      setTimeout(() => {
        this.refresh()
      }, this.refreshDelay)
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->

第三步:组件应用

<template>
<div class="scrollWrapper">
    <scroll ref="scroll"
        :data="lists"
    >
        <div>
            <div class="item" v-for="(item, index) in lists" :key="index">{{item}}</div>
        </div>
    </scroll>
</div>
</template>
<script>
import scroll from '@/components/scroll/scroll'
export default {
    name: '',
    data() {
        return {
            lists: [1, 2, 3, 4, 5, 6, 7, 8, 9]
        }
    },
    components: {
        scroll
    },
    created() {

    },
    mounted() {

    },
    methods: {

    },
    computed: {

    }
}
</script>
<style lang="less" scoped>
.scrollWrapper {
    width: 100%;
    overflow: hidden;
    height: 100%;
    position: relative;
    &>div:nth-of-type(1) {
        width: 100%;
        height: 100%;
        position: absolute;
        .item {
            width: 100%;
            height: 100px;
            background-color: antiquewhite;
            margin: 5px auto;
        }
    }
    
}
</style>


相关文章

网友评论

      本文标题:vue应用better-scroll

      本文链接:https://www.haomeiwen.com/subject/fvtscctx.html