有关better-scroll的左右联动问题(层及关联)

1:better-scroll

//npm install better-scroll --save
import BetterScroll from 'better-scroll'
//一般在vue组件里面进行数据监视   this.$nextTick(()=>{ 代码块})
initScroll () {
	this.leftScroll = new BetterScroll('this.$refs.refName',{
		click:true
	})
	this.rightScroll = new BetterScroll('this.$refs.refName',{
		click:true
		//值为  0  1触摸  2触摸  实时分发   3 触摸/惯性
		probeType:1
	})
}

2:左右菜单联动时(左右两边相互牵连)/滑动右侧列表左侧跟着变化

2.1:滑动右侧列表左侧分类可能变化
2.2:点击左侧分类右侧滑动到对应位置

//一般需要设计两套数组数组
//设计当前分类的下标:currentIndex(计算属性);
//scrollY 右侧列表滑动的Y轴坐标--初始值为0,在滑动过程中实时改变
//tops:右侧所有分类的top组成的数组--在列表显示之后统计一次即可
let scrollY = 0
let tops = []

//计算属性内的currentIndex

currentIndex () {
	const {scrollY,tops} = this
	const index = tops.findIndex((top,index)=>scrollY>=top&&scroll<tops[index+1])
	if(index!=this.index &&this.leftScroll){
		this.index = index
		const li = this.$refs.leftUrl.children[index]
		this.leftScroll.scrollToElement(li,500)
	}
	return index
}
//tops在mounted里面   if(){}
this.initTops()
//methods里面定义初始化tops数组判定左侧列表的高度
initTops () {
	const tops = []
	let top = 0
	tops.push(top)
	const this.$refs.refName.children
	Array.prototype.forEach.call(lis,(li)=>{
		top += li.clientHeight
		tops.push(top)
	})
	this.tops = tops
} 
//绑定时间监听better-scroll内部给的绑定方法以及相应的回调函数以及事件参数
this.rightScroll.on('scroll',({x,y})=>{
	console.log(x,y)
	this.scrollY = Math.abs(y)
})
this.rightScroll.on('scrollEnd',({x,y})=>{
	console.log(x,y)
	this.scrollY = Math.abs(y)
})

3:左侧列表的改变

//给列表item绑定点击事件 @click="selectItem(index)"
//回调函数methods
selectItem (index) {
	//让右侧列表滑动到对应位置
	const top = this.tops[index]
	//立即更新scrollY
	this.scrollY = top
	this.rightScroll.scrollTo(0,-top,500)
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值