Vue實(shí)現(xiàn)左右菜單聯(lián)動(dòng)實(shí)現(xiàn)代碼
本文介紹了Vue實(shí)現(xiàn)左右菜單聯(lián)動(dòng)實(shí)現(xiàn)代碼嗎,分享給大家,也給自己留個(gè)筆記,具體如下:
源碼傳送門: Rain120/vue-study
之前在外賣軟件上看到這個(gè)左右聯(lián)動(dòng)的效果,覺得很有意思,所以就嘗試使用 Vue 來實(shí)現(xiàn),將這個(gè)聯(lián)動(dòng)抽離成為一個(gè)單獨(dú)的組件,廢話少說,先來一張效果圖。

這個(gè)組件分為兩個(gè)部分,1、左菜單;2、右菜單。 左菜單的 DOM 結(jié)構(gòu)
<scroll
class="left-menu"
:data="leftMenu"
ref="leftMenu">
<div class="left-menu-container">
<ul>
<li
class="left-item"
ref="leftItem"
:class="{'current': currentIndex === index}"
@click="selectLeft(index, $event)"
v-for="(item, index) in leftMenu"
:key="index">
<p class="text">{{item}}</p>
</li>
</ul>
</div>
</scroll>
右菜單的 DOM 結(jié)構(gòu)
<scroll
class="right-menu"
:data="rightMenu"
ref="rightMenu"
@scroll="scrollHeight"
:listenScroll="true"
:probeType="3">
<div class="right-menu-container">
<ul>
<li class="right-item" ref="rightItem" v-for="(items, i) in rightMenu" :key="i">
<div class="data-wrapper">
<div class="title">{{items.title}}</div>
<div class="data" v-for="(item, j) in items.data" :key="j">{{item}}</div>
</div>
</li>
</ul>
</div>
</scroll>
這里是為了做 demo ,所以在數(shù)據(jù)上只是單純捏造。
當(dāng)然因?yàn)檫@是個(gè)子組件,我們將通過父組件傳遞 props ,所以定義 props
props: {
leftMenu: {
required: true,
type: Array,
default () {
return []
}
},
rightMenu: {
required: true,
type: Array,
default () {
return []
}
},
}

在這個(gè)業(yè)務(wù)場(chǎng)景中,我們的實(shí)現(xiàn)方式是根據(jù)右邊菜單滾動(dòng)的高度來計(jì)算左邊菜單的位置,當(dāng)然左邊菜單也可以通過點(diǎn)擊來確定右邊菜單需要滾動(dòng)多高的距離,那么我們?nèi)绾潍@得該容器滾動(dòng)的距離呢? 之前一直在使用better-scroll,通過閱讀文檔,我們知道它有有 scroll 事件,我們可以通過監(jiān)聽這個(gè)事件來獲取滾動(dòng)的 pos

if (this.listenScroll) {
let me = this
this.scroll.on('scroll', (pos) => {
me.$emit('scroll', pos)
})
}
所以我們?cè)谟疫叢藛蔚?scroll 組件上監(jiān)聽scroll事件
@scroll="scrollHeight"
method
scrollHeight (pos) {
console.log(pos);
this.scrollY = Math.abs(Math.round(pos.y))
},
我們將監(jiān)聽得到的pos打出來看看

我們可以看到控制臺(tái)打出了當(dāng)前滾動(dòng)的pos信息,因?yàn)樵谝苿?dòng)端開發(fā)時(shí),坐標(biāo)軸和我們數(shù)學(xué)中的坐標(biāo)軸相反,所以上滑時(shí)y軸的值是負(fù)數(shù)

所以我們要得到每一塊 li 的高度,我們可以通過拿到他們的 DOM
_calculateHeight() {
let lis = this.$refs.rightItem;
let height = 0
this.rightHeight.push(height)
Array.prototype.slice.call(lis).forEach(li => {
height += li.clientHeight
this.rightHeight.push(height)
})
console.log(this.rightHeight)
}
我們?cè)?created 這個(gè) hook 之后調(diào)用這個(gè)計(jì)算高度的函數(shù)
_calculateHeight() {
let lis = this.$refs.rightItem;
let height = 0
this.rightHeight.push(height)
Array.prototype.slice.call(lis).forEach(li => {
height += li.clientHeight
this.rightHeight.push(height)
})
console.log(this.rightHeight)
}

當(dāng)用戶在滾動(dòng)時(shí),我們需要計(jì)算當(dāng)前滾動(dòng)距離實(shí)在那個(gè)區(qū)間內(nèi),并拿到他的 index


computed: {
currentIndex () {
const { scrollY, rightHeight } = this
const index = rightHeight.findIndex((height, index) => {
return scrollY >= rightHeight[index] && scrollY < rightHeight[index + 1]
})
return index > 0 ? index : 0
}
}
所以當(dāng)前應(yīng)該是左邊菜單 index = 1 的菜單項(xiàng) active 以上是左邊菜單根據(jù)右邊菜單的滑動(dòng)聯(lián)動(dòng)的實(shí)現(xiàn),用戶也可以通過點(diǎn)擊左邊菜單來實(shí)現(xiàn)右邊菜單的聯(lián)動(dòng),此時(shí),我們給菜單項(xiàng)加上 click事件
@click="selectLeft(index, $event)"
這里加上 $event 是為了區(qū)分原生點(diǎn)擊事件還是[better-scroll]((https://ustbhuangyi.github.io/better-scroll/doc/zh-hans/#better-scroll 是什么)派發(fā)的事件
selectLeft (index, event) {
if (!event._constructed) {
return
}
let rightItem = this.$refs.rightItem
let el = rightItem[index]
this.$refs.rightMenu.scrollToElement(el, 300)
},
到這里我們就基本上完成了這些需求了
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- vue省市區(qū)三聯(lián)動(dòng)下拉選擇組件的實(shí)現(xiàn)
- vue elementUI使用tabs與導(dǎo)航欄聯(lián)動(dòng)
- vue基于mint-ui的城市選擇3級(jí)聯(lián)動(dòng)的示例
- vue基于mint-ui實(shí)現(xiàn)城市選擇三級(jí)聯(lián)動(dòng)
- 詳解Vue、element-ui、axios實(shí)現(xiàn)省市區(qū)三級(jí)聯(lián)動(dòng)
- VUE2 前端實(shí)現(xiàn) 靜態(tài)二級(jí)省市聯(lián)動(dòng)選擇select的示例
- Vue.js組件tree實(shí)現(xiàn)省市多級(jí)聯(lián)動(dòng)
- vue mint-ui 實(shí)現(xiàn)省市區(qū)街道4級(jí)聯(lián)動(dòng)示例(仿淘寶京東收貨地址4級(jí)聯(lián)動(dòng))
- vue左右側(cè)聯(lián)動(dòng)滾動(dòng)的實(shí)現(xiàn)代碼
- vue實(shí)現(xiàn)列表左右聯(lián)動(dòng)效果
相關(guān)文章
vue實(shí)現(xiàn)集成騰訊TIM即時(shí)通訊
最近在做商城類的項(xiàng)目,需要使用到客服系統(tǒng),用戶選擇的騰訊IM即時(shí)通信,所以本文主要介紹了vue實(shí)現(xiàn)集成騰訊TIM即時(shí)通訊,感興趣的可以了解一下2021-06-06
vue 實(shí)現(xiàn)cli3.0中使用proxy進(jìn)行代理轉(zhuǎn)發(fā)
今天小編就為大家分享一篇vue 實(shí)現(xiàn)cli3.0中使用proxy進(jìn)行代理轉(zhuǎn)發(fā),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-10-10
vue之字符串、數(shù)組之間的相互轉(zhuǎn)換方式
這篇文章主要介紹了vue之字符串、數(shù)組之間的相互轉(zhuǎn)換方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07
Ant Design Vue table組件如何自定義分頁器
這篇文章主要介紹了Ant Design Vue table組件如何自定義分頁器問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04
Vue+ElementUi實(shí)現(xiàn)點(diǎn)擊表格鏈接頁面跳轉(zhuǎn)和路由效果
這篇文章主要介紹了Vue+ElementUi實(shí)現(xiàn)點(diǎn)擊表格中鏈接進(jìn)行頁面跳轉(zhuǎn)和路由,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-03-03

