美文网首页
Vue中二次封装better-scroll插件

Vue中二次封装better-scroll插件

作者: 切图仔阿庄 | 来源:发表于2019-03-10 15:50 被阅读0次

    1.用npm 安装好 better-scroll

    npm install--save better-scroll

    2.新建公共组件scroll

    <template>
      <div ref="wrapper">
        <slot></slot>
      </div>
    </template>
    
    <script type="text/ecmascript-6">
    import BScroll from 'better-scroll';
    
    export default {
      name: 'scrollComm',
      props: {
        probeType: {
          type: Number,
          default: 1
        },
        click: {
          type: Boolean,
          default: true
        },
        //scroll 通常会有循环的数组对象
        data: { 
          type: Array,
          default: null
        },
        // 监听
        listenScroll: {
          type: Boolean,
          default: false
        },
        // 是否需要上啦刷新
        pullup: {
          type: Boolean,
          default: false
        },
        // 处理移动端 input输入时在滚动列表 键盘不回收起问题
        beforeScroll: {
          type: Boolean,
          default: false
        },
        refreshDelay: {
          type: Number,
          default: 20
        }
      },
      mounted() {
        setTimeout(() => {
          // eslint-disable-next-line
          this._initScroll();
        }, 20);
      },
      methods: {
        _initScroll() {
          if (!this.$refs.wrapper) {
            return;
          }
          this.scroll = new BScroll(this.$refs.wrapper, {
            probeType: this.probeType,
            click: this.click,
            bounce: false
          });
          if (this.listenScroll) {
            const me = this;
            this.scroll.on('scroll', (pos) => {
              me.$emit('scroll', pos);
            });
          }
          if (this.pullup) {
            // scrollEnd表示scroll停止了,scrollToEnd表示滚动到底部了
            this.scroll.on('scrollEnd', () => {
              if (this.scroll.y <= (this.scroll.maxScrollY + 50)) {
                this.$emit('scrollToEnd');
              }
            });
          }
    
          if (this.beforeScroll) {
            this.scroll.on('beforeScrollStart', () => {
              this.$emit('beforeScroll');
            });
          }
        },
        disable() {
          this.scroll && this.scroll.disable();
        },
        enable() {
          this.scroll && this.scroll.enable();
        },
        refresh() {
          this.scroll && this.scroll.refresh();
        },
        scrollTo() {
          // eslint-disable-next-line
          this.scroll && this.scroll.scrollTo.apply(this.scroll, arguments);
        },
        scrollToElement() {
          // eslint-disable-next-line
          this.scroll && this.scroll.scrollToElement.apply(this.scroll, arguments);
        },
        finishPullUp() {
          this.scroll && this.scroll.finishPullUp();
        }
      },
      watch: {
        data() {
          setTimeout(() => {
            this.refresh();
          }, this.refreshDelay);
        }
      }
    };
    </script>
    

    ToT这样一个组件就写好辣。。。

    相关文章

      网友评论

          本文标题:Vue中二次封装better-scroll插件

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