mpvue實現(xiàn)左側(cè)導航與右側(cè)內(nèi)容的聯(lián)動
本文實例為大家分享了mpvue左側(cè)導航與右側(cè)內(nèi)容聯(lián)動的具體代碼,供大家參考,具體內(nèi)容如下
效果圖如下:

(1)左側(cè)導航聯(lián)動右側(cè)內(nèi)容
實現(xiàn):點擊左側(cè)導航,右側(cè)內(nèi)容滑到對應的位置,并且導航上有current當前樣式。
mpvue用的還是微信小程序提供的組件scroll-view,它里面有一個屬性scroll-into-view,值為某子元素的id,滾動到該元素。
template:
<scroll-view class="menu-wrapper" scroll-y>
<ul>
<li class="menu-item"
v-for="(item,index) in goods"
:class="index===currentIndex ? 'current' : ''"
:key="index"
@tap="selectMenu(index)">
{{item.name}}
</li>
</ul>
</scroll-view>
<scroll-view scroll-y
:scroll-into-view="contentId"
scroll-with-animation="true"
class="foods-wrapper">
<ul>
<li v-for="(item,i) in goods"
:id="'con_'+i"
class="food-list food-list-hook" :key="i">
</li>
</ul>
<scroll-view>
js:
data() {
return {
goods: [],
contentId: '', // 每個food-list的id,scroll-into-view滾動到對應的id
currentIndex: 0
}
},
methods: {
selectMenu(index) {
this.contentId = `con_${index}`
this.currentIndex = index
}
}
(2)在左側(cè)導航聯(lián)動右側(cè)內(nèi)容的基礎上增加右側(cè)內(nèi)容聯(lián)動左側(cè)導航
實現(xiàn):滑動右側(cè)內(nèi)容區(qū)域,給左側(cè)對應導航增加current樣式,并且當導航高度過長,會聯(lián)動其滾動
補充:contentHeight是右側(cè)內(nèi)容scroll-view的高度,同時也是左側(cè)導航scroll-view的高度,navItemHeight是導航ul下每一個item的高度,當導航下ul的高度超過scroll-view的高度,并且this.currentIndex * this.navItemHeight > this.contentHeight,導航才向上滾動。
tempate:
<scroll-view class="menu-wrapper"
:scroll-into-view="navId"
scroll-with-animation="true"
scroll-y>
<ul class="menu-ul">
<li class="menu-item"
v-for="(item,index) in goods"
:id="'nav_'+index"
:class="index===currentIndex ? 'current' : ''"
:key="index"
@tap="selectMenu(index)">
{{item.name}}
</li>
</ul>
</scroll-view>
<scroll-view scroll-y
@scroll="onScroll"
:scroll-into-view="contentId"
scroll-with-animation="true"
class="foods-wrapper">
<ul>
<li v-for="(item,i) in goods"
:id="'con_'+i"
class="food-list food-list-hook" :key="i">
</li>
</ul>
</scroll-view>
js:
export default{
data() {
return {
goods: [],
contentId: '', // 每個food-list的id,scroll-into-view滾動到對應的id
navId: '', // 導航模塊對應的id,用來聯(lián)動內(nèi)容區(qū)域
currentIndex: 0,
navulHeight: 0, // 導航里ul高度
navItemHeight: 0, // 每個導航高度
listHeight: [], // foods內(nèi)部塊的高度
contentHeight: [], // 內(nèi)容區(qū)域scroll-view高度
}
},
watch: {
currentIndex() {
console.log(this.currentIndex)
if (this.contentHeight < this.navulHeight) {
let h = this.currentIndex * this.navItemHeight
if (h > this.contentHeight) {
// 導航滑動
this.navId = `nav_${this.currentIndex}`
} else {
this.navId = 'nav_0'
}
}
}
},
methods: {
selectMenu(index) {
this.contentId = `con_${index}`
this.navId = `nav_${index}`
this.currentIndex = index
},
onScroll(e) {
this.contentId = ''
let scrollTop = e.target.scrollTop
// console.log(scrollTop)
let length = this.listHeight.length
if (scrollTop >= this.listHeight[length - 1] - this.contentHeight) {
return
} else if (scrollTop > 0 && scrollTop < this.listHeight[0]) {
this.currentIndex = 0
}
for (let i = 0; i < length; i++) {
if (scrollTop >= this.listHeight[i - 1] && scrollTop < this.listHeight[i]) {
this.currentIndex = i
}
}
// console.log(this.currentIndex)
},
getFoodHeight() {
var query = wx.createSelectorQuery()
let h = 0
query.selectAll('.food-list-hook').boundingClientRect((rects) => {
// console.log(rects)
rects.forEach((rect) => {
h += rect.height
this.listHeight.push(h)
})
// console.log(this.listHeight)
})
query.select('.foods-wrapper').boundingClientRect((rect) => {
this.contentHeight = rect.height
})
query.select('.menu-ul').boundingClientRect((rect) => {
this.navulHeight = rect.height
})
query.select('.menu-item').boundingClientRect((rect) => {
this.navItemHeight = rect.height
}).exec()
}
},
watch: {
goods() {
// 獲取模塊高度,即food-list
setTimeout(() => {
this.getFoodHeight()
}, 60)
}
}
}
更多教程點擊《Vue.js前端組件學習教程》,歡迎大家學習閱讀。
關于vue.js組件的教程,請大家點擊專題vue.js組件學習教程進行學習。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
使用vue-router beforEach實現(xiàn)判斷用戶登錄跳轉(zhuǎn)路由篩選功能
這篇文章主要介紹了vue使用vue-router beforEach實現(xiàn)判斷用戶登錄跳轉(zhuǎn)路由篩選,本篇文章默認您已經(jīng)會使用 webpack 或者 vue-cli 來進行環(huán)境的搭建,并且具有一定的vue基礎。需要的朋友可以參考下2018-06-06
vue-element-admin按鈕級權限管控的實現(xiàn)
開發(fā)離不開權限,不同的用戶登錄,根據(jù)不同的權限,可以訪問不同的管理目錄,本文主要介紹了vue-element-admin按鈕級權限管控的實現(xiàn),具有一定的參考價值,感興趣的可以了解一下2022-04-04
vue中監(jiān)聽input框獲取焦點及失去焦點的問題
這篇文章主要介紹了vue中監(jiān)聽input框獲取焦點,失去焦點的問題,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-07-07

