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)
}