Vue中better-scroll实现左右侧BScroll联动nextTick

本文介绍如何在Vue项目中使用better-scroll实现在移动端实现左侧和右侧滚动条的联动效果,通过nextTick确保滚动更新的同步性。

better-scroll用法实现左右侧关联

Vue中better-scroll实现左右侧联动


```javascript
<script>
import BScroll from 'better-scroll'
export default {
  watch: {
    menus() {
      this.$nextTick(() => {        
        this._initRightLiTops()
this._initBScroll()
      })
    },
  },
  methods: {
    _initBScroll() {
      this.leftBScroll = new BScroll('.menu-left', {
        click: true,
        mouseWheel: true,
        probeType: 3,
      })
      this.rightBScroll = new BScroll('.menu-right', {
        click: true,
        mouseWheel: true,
        probeType: 3,
      })
      this.rightBScroll.on('scroll', (pos) => {
        var scrollY = Math.abs(pos.y)
        for (var i = 0; i < this.rightLiTops.length - 1; i++) {
          if (
            scrollY >= this.rightLiTops[i] &&
            scrollY < this.rightLiTops[i + 1]
          ) {
            this.currentIndex = i
          }
        }
        var el = this.$refs.cate[this.currentIndex]
        this.leftBScroll.scrollToElement(el, 300, 0, -100)
      })
    },
    _initRightLiTops() {
      var top = 0
      var topArr = []
      topArr.push(top)
      this.$refs.cate.forEach((item) => {
        top += item.clientHeight
        topArr.push(top)
      })
      this.rightLiTops = topArr
      var bottom = this.$refs.itemList.getElementsByClassName('cate-bottom')[0]
      document.getElementsByClassName('category-info')[0].style.height = document.documentElement.clientHeight + 'px'
      console.log(document.body.clientHeight, 'document.body.clientHeight')
      bottom.style.height = this.$refs.itemList.clientHeight + 10 - this.$refs.cate[this.rightLiTops.length - 2].clientHeight +'px'
    }
  }
}
</script>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值