{{food.name}}
{{food.description}}
什么是 better-scroll
better-scroll 是一个移动端滚动的解决方案,它是基于 iscroll 的重写,它和 iscroll 的主要区别在这里 。better-scroll 也很强大,不仅可以做普通的滚动列表,还可以做轮播图、picker 等等。
与1.0版本不同的是,我们使用的是ref
很简单这样页面就可以滚动了,如下图
但是,这样左右两个页面并没有联动起来,需要我们再定义一个方法来计算滚动的高度,以及在计算属性中计算左侧当前索引在哪里
从而定义左侧边栏的位置
computed:{ //用来计算左侧当前索引在哪,从而定位到左侧边栏的位置 currentIndex(){ for (let i = 0; i < this.listHeight.length; i++) { var height1=this.listHeight[i] ; var height2=this.listHeight[i+1]; if(!height2||(this.scrollY >= height1 && this.scrollY < height2)){ return i; } } return 0; } }, methods:{ _initScroll(){ // 使用better-scroll实现的关键代码 this.menuScroll=new BScroll(this.$refs.menuWrapper,{click:true}); this.foodScroll=new BScroll(this.$refs.foodWrapper,{ click: true, //探针作用,实时监测滚动位置 probeType: 3 }); this.foodScroll.on('scroll',(pos)=>{ this.scrollY=Math.abs(Math.round(pos.y)) }); }, _calculateHeight(){ let foodList=this.$refs.foodWrapper.getElementsByClassName('food-list-hook'); let ; this.listHeight.push(height); for (var i = 0; i < foodList.length; i++) { let item=foodList[i]; height+=item.clientHeight; this.listHeight.push(height); } } } //dom结构加载结束(nextTick这个接口是计算dom相关的,要确保原生dom已经渲染了) this.$nextTick(()=>{ this._initScroll(); this._calculateHeight(); });
在dom渲染后,也是需要计算高度的.
滑动右边,实现左边联动已经实现了,接下来就是,点击左边的菜单,右边的食物相应滚动到具体的位置
给左边菜单绑定一个事件:@click="selectMenu(index,$event)"
/左边的菜单项的点击事件 selectMenu(index,event){ //自己默认派发事件时(BScroll),_constructed默认为true.但原生的浏览器并没有这个属性 if (!event._constructed) { return; } //运用BScroll滚动到相应位置 //运用index去找到对应的右侧位置 let foodList=this.$refs.foodWrapper.getElementsByClassName('food-list-hook'); //滚动到相应的位置 let el=foodList[index]; //设置滚动时间 this.foodScroll.scrollToElement(el,2000); }
至此,整个联动实现的,完整代码如下
{{item.name}}
![]()
{{food.name}}
{{food.description}}
月售{{food.sellCount}}份 好评率{{food.rating}}%¥{{food.price}}¥{{food.oldPrice}}
茶杯头甜蜜终章dlc 官方手机版v1.0.0.3
下载火柴人传说暗影格斗内置菜单 最新版v3.0.1
下载荒野乱斗测试服 安卓版v61.10.3
下载荒野乱斗彩虹服 安卓版v61.10.3
下载寒霜启示录 安卓版v1.25.10
寒霜启示录是一款生存模拟游戏,不少玩家可能对于末日都有着自己
末日城堡免广告版 安卓最新版v0.7.1
末日城堡免广告版是一款非常好玩的模拟经营类游戏,内部可以不看
甜蜜人生模拟器 最新版v1.4.5
甜蜜人生模拟器是一款非常好玩的模拟恋爱手游,玩家在这里能够对
武器锻造师内置功能菜单 v10.4
武器锻造师内置菜单版是游戏的破解版本,在该版本中为玩家提供了
开放空间overfield 安卓版v1.0.5
开放空间Overfield是一款箱庭养成经营手游,让你在广阔