vue和better-scroll實現(xiàn)列表左右聯(lián)動效果詳解
一.實現(xiàn)思路
- (1)實現(xiàn)上是左右分別一個better-scroll列表
- (2)利用計算右側列表每一個大區(qū)塊的高度來計算左側的位置
二.實現(xiàn)
1.實現(xiàn)左右兩個better-scroll
(1)dom結構(better-scroll要求,會把最外層dom的第一個子元素作為要滾動的區(qū)域)
左邊滾動列表dom
<div class="menu-wrapper" v-el:menu-wrapper>
<ul>
<li v-for="item in goods" class="menu-item"
:class="{'current':currentIndex === $index}"
@click="selectMenu($index,$event)">
<span class="text border-1px">
<span v-show="item.type > 0" class="icon"
:class="classMap[item.type]"></span>{{item.name}}
</span>
</li>
</ul>
</div>
右邊滾動列表dom
<div class="food-wrapper" v-el:food-wrapper>
<ul>
<li v-for="item in goods" class="food-list food-list-hook">
<h1 class="title">{{item.name}}</h1>
<ul>
<li v-for="food in item.foods" class="food-item border-1px">
<div class="icon">
<img width="57" height="57" :src="food.icon">
</div>
<div class="content">
<h2 class="name">{{food.name}}</h2>
<p class="desc">{{food.description}}</p>
<div class="extra">
<span class="count">月售{{food.sellCount}}份</span>
<span>好評率{{food.rating}}%</span>
<div class="price">
<span class="now">¥{{food.price}}</span>
<span class="old" v-show="food.oldPrice">¥{{food.oldPrice}}</span>
</div>
</div>
</div>
</li>
</ul>
</li>
</ul>
</div>
在數(shù)據(jù)請求完成后的$nextTick中初始化better-scroll,就能實現(xiàn)兩個列表分別能滾動,至于聯(lián)動,要后面自己做
_initScroll() {
this.menuScroll = new BScroll(this.$els.menuWrapper,{
click:true //允許better-scroll列表上的點擊事件
});
this.foodsScroll = new BScroll(this.$els.foodWrapper,{
probeType : 3 //讓better-scroll監(jiān)聽scroll事件
});
this.foodsScroll.on('scroll',(pos) => {
this.scrollY =Math.abs(Math.round(pos.y));
})
},
2.實現(xiàn)聯(lián)動效果
(1)具體的聯(lián)動實現(xiàn)思路
- 在渲染完成后($nextTick內),初始化better-scroll,并在初始化函數(shù)內添加右側列表的scroll監(jiān)聽事件,并記錄scrollY值到,存入vue的data中
- 在渲染完成后($nextTick內),計算右側列表的每一個大區(qū)塊的高度,并累加,存入數(shù)組listHeight
- 因為scrollY值在滾動中總是不斷變化的,所以在computed中計算出currentIndex,當前滾動區(qū)域是哪一個大區(qū)塊,也就是listHeight數(shù)組的下標
- 在dom中根據(jù)currentIndex應用左側列表被點中的樣式
- 在左側列表某一項被點中的時候,右側列表滑動到某一個大塊區(qū)域,
//初始化better-scroll
_initScroll() {
this.menuScroll = new BScroll(this.$els.menuWrapper,{
click:true
});
this.foodsScroll = new BScroll(this.$els.foodWrapper,{
probeType : 3
});
this.foodsScroll.on('scroll',(pos) => {
this.scrollY =Math.abs(Math.round(pos.y));
})
},
_calculateHeight() {
let foodList = this.$els.foodWrapper.getElementsByClassName("food-list-hook");
let height = 0;
this.listHeight.push(height);
for(let i=0;i<foodList.length;i++) {
let item = foodList[i];
height += item.clientHeight;
this.listHeight.push(height);
}
}
computed: {
currentIndex() {
for(let i=0;i< this.listHeight.length;i++) {
let height1 = this.listHeight[i];
let height2 = this.listHeight[i+1];
if(!height2 || (this.scrollY >= height1 && this.scrollY < height2)){
return i;
}
}
return 0;
}
},
<div class="menu-wrapper" v-el:menu-wrapper>
<ul>
<!-- :class="{'current':currentIndex === $index}" 就是根據(jù)currentIndex應用左側列表被點中的樣式 -->
<li v-for="item in goods" class="menu-item"
:class="{'current':currentIndex === $index}"
@click="selectMenu($index,$event)">
<span class="text border-1px">
<span v-show="item.type > 0" class="icon"
:class="classMap[item.type]"></span>{{item.name}}
</span>
</li>
</ul>
</div>
//被點擊事件
//dom
<div class="menu-wrapper" v-el:menu-wrapper>
<ul>
<!-- @click="selectMenu($index,$event)" 就是點擊事件 -->
<li v-for="item in goods" class="menu-item"
:class="{'current':currentIndex === $index}"
@click="selectMenu($index,$event)">
<span class="text border-1px">
<span v-show="item.type > 0" class="icon"
:class="classMap[item.type]"></span>{{item.name}}
</span>
</li>
</ul>
</div>
//js
selectMenu(index,event) {
if(!event._constructed) {
return ;
}
let foodList = this.$els.foodWrapper.getElementsByClassName("food-list-hook");
let el = foodList[index];
this.foodsScroll.scrollToElement(el,300);
},
以上所述是小編給大家介紹的vue和better-scroll實現(xiàn)列表左右聯(lián)動效果詳解整合,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
- 詳解 vue better-scroll滾動插件排坑
- vue滾動軸插件better-scroll使用詳解
- vue利用better-scroll實現(xiàn)輪播圖與頁面滾動詳解
- vue滾動插件better-scroll使用詳解
- vue使用 better-scroll的參數(shù)和方法詳解
- vue.js2.0 實現(xiàn)better-scroll的滾動效果實例詳解
- vue中使用better-scroll實現(xiàn)滑動效果及注意事項
- vue better-scroll插件使用詳解
- vue2.0 better-scroll 實現(xiàn)移動端滑動的示例代碼
- Vue中利用better-scroll組件實現(xiàn)橫向滾動功能
相關文章
前端vue?a鏈接下載文件失敗的問題(未發(fā)現(xiàn)文件)
這篇文章主要介紹了前端vue?a鏈接下載文件失敗的問題(未發(fā)現(xiàn)文件),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09
vue element ui使用選擇器實現(xiàn)地區(qū)選擇兩種方法
這篇文章主要給大家介紹了關于vue element ui使用選擇器實現(xiàn)地區(qū)選擇的兩種方法,Element UI是一套基于Vue.js開發(fā)的UI組件庫,其中包含了地區(qū)選擇器(Cascader)組件,需要的朋友可以參考下2023-09-09
electron-vite工具打包后如何通過內置配置文件動態(tài)修改接口地址
使用electron-vite?工具開發(fā)項目打包完后每次要改接口地址都要重新打包,對于多環(huán)境切換或者頻繁變更接口地址就顯得麻煩,這篇文章主要介紹了electron-vite工具打包后通過內置配置文件動態(tài)修改接口地址實現(xiàn)方法,需要的朋友可以參考下2024-05-05

