Better-scroll 移动端Slide 组件 不能loop

新手上路,请多包涵

如题,设置loop 初始化时候不生效
使用的是官网的demo组件

组件代码

<template>
<!-- 竖屏滑动组件 -->
  <div class="slide" ref="slide">
    <div class="slide-group" ref="slideGroup">
      <slot>
      </slot>
    </div>
  </div>
</template>

<script type="text/ecmascript-6">
  import BScroll from 'better-scroll'
  const COMPONENT_NAME = 'slide'
  export default {
    name: COMPONENT_NAME,
     props: {
      dataInfo: {
        type: Array, 
      },
      loop: {
        type: Boolean,
        default: true
      },
      autoPlay: {
        type: Boolean,
        default: false
      },
      interval: {
        type: Number,
        default: 4000
      },
      click: {
        type: Boolean,
        default: true
      },
    },
    data() {
      return {
        currentPageIndex: 0
      }
    },
    watch: {
      dataInfo () {
        // 监听数据变化,重新计算scroll
        setTimeout(() => {
          this.refresh()
        }, 60)
      }
    },
    mounted() {
      this.update()
      window.addEventListener('resize', () => {
        if (!this.slide || !this.slide.enabled) {
          return
        }
        clearTimeout(this.resizeTimer)
        this.resizeTimer = setTimeout(() => {
          if (this.slide.isInTransition) {
            this._onScrollEnd()
          } else {
            if (this.autoPlay) {
              this._play()
            }
          }
          this.refresh()
        }, 60)
      })
    },
    activated() {
      if (!this.slide) {
        return
      }
      this.slide.enable()
      let pageIndex = this.slide.getCurrentPage().pageY
      this.slide.goToPage(0, pageIndex, 0)
      this.currentPageIndex = pageIndex
      if (this.autoPlay) {
        this._play()
      }
    },
    deactivated() {
      this.slide.disable()
      clearTimeout(this.timer)
    },
    beforeDestroy() {
      this.slide.disable()
      clearTimeout(this.timer)
    },
    methods: {
      update() {
        if (this.slide) {
          this.slide.destroy()
        }
        this.$nextTick(() => {
          this.init()
        })
      },
      refresh() {
        this._setSlideHeight(true)
      },
      next() {
        this.slide.next()
      },
      init() {
        clearTimeout(this.timer)
        this.currentPageIndex = 0
        this._initSlide()
        this._setSlideHeight()
        if (this.autoPlay) {
          this._play()
        }
      },
      _setSlideHeight(isResize) {
        this.children = this.$refs.slideGroup.children
        let height = 0
        let slideHeight = this.$refs.slide.clientHeight
        for (let i = 0; i < this.children.length; i++) {
          let child = this.children[i]
          child.style.height = slideHeight + 'px'
          height += slideHeight
        }
        if (this.loop && !isResize) {
          height += 2 * slideHeight
        }
        this.$refs.slideGroup.style.height = height + 'px'
      },
      _initSlide() {
        this.slide = new BScroll(this.$refs.slide, {
          scrollX: false,
          scrollY: true,
          momentum: false,
          stopPropagation: true,
          snap: {
            loop: false,
            threshold: 0.1,
            speed: 400,
            easing: {
              style: 'cubic-bezier(0.275, 0.885, 0.32, 1.275)'
            }
          },
          bounce: false,
          click: this.click,
        })
        this.slide.on('scrollEnd', this._onScrollEnd)
        this.slide.on('touchEnd', () => {
          if (this.autoPlay) {
            this._play()
          }
        })
        this.slide.on('beforeScrollStart', () => {
          if (this.autoPlay) {
            clearTimeout(this.timer)
          }
        })
      },
      _onScrollEnd() {
        let pageIndex = this.slide.getCurrentPage().pageY
        this.currentPageIndex = pageIndex
        if (this.autoPlay) {
          this._play()
        }
      },
      _play() {
        clearTimeout(this.timer)
        this.timer = setTimeout(() => {
          this.slide.next()
        }, this.interval)
      }
    }
  }
</script>
<style lang="less" scoped>
.slide {
  min-height: 1px;
  height: 100vh;
  overflow: hidden;
  .slide-group {
    position: relative;
  }
}
</style>

使用组件

 <slide :dataInfo="infoData.SectionList"  ref="$Slide">
          <template v-if="infoData.Cover_url">
                <component  :is="infoData.Invitation_Style_Code" :BasicInfo="BasicInfo"     
            :enrollCount="infoData.EnrollCount" v-on:linkto="linkto" 
       :backgroundUrl="infoData.Cover_url" :isCostom="infoData.Custom_App_Template"></component>
      </template>
</slide>

另外初始化后在修改数据不刷新页面,会出一个克隆元素 但是数据没有克隆进去。

阅读 2.2k
1 个回答

第一个问题,你描述的不是很清楚,不是太明白
第二个问题应该你在vue中使用的时候没有注意对数组处理的方法,可以再仔细看看,我在使用 better-scroll的时候没有遇到这样的问题哦,还有如果是你只是简单使用上拉加载下拉刷新的功能,并且是vue的话你可以直接使用vue-scroll;
如果你执意使用better-scroll建议你看下这个文档使用说明书

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题