欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

mpvue實現(xiàn)左側導航與右側內容的聯(lián)動

 更新時間:2019年10月21日 09:58:54   作者:yumihe  
這篇文章主要為大家詳細介紹了mpvue實現(xiàn)左側導航與右側內容的聯(lián)動,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了mpvue左側導航與右側內容聯(lián)動的具體代碼,供大家參考,具體內容如下

效果圖如下:

(1)左側導航聯(lián)動右側內容

實現(xiàn):點擊左側導航,右側內容滑到對應的位置,并且導航上有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)在左側導航聯(lián)動右側內容的基礎上增加右側內容聯(lián)動左側導航

實現(xiàn):滑動右側內容區(qū)域,給左側對應導航增加current樣式,并且當導航高度過長,會聯(lián)動其滾動

補充:contentHeight是右側內容scroll-view的高度,同時也是左側導航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)動內容區(qū)域
 currentIndex: 0,
 navulHeight: 0, // 導航里ul高度
 navItemHeight: 0, // 每個導航高度
 listHeight: [], // foods內部塊的高度
 contentHeight: [], // 內容區(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組件學習教程進行學習。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • vue項目npm?run?build打包dist文件及打包后空白解決辦法

    vue項目npm?run?build打包dist文件及打包后空白解決辦法

    npm run build 這個命令會執(zhí)行Vue CLI中預定義的打包配置,并將打包后的文件存放在"dist"文件夾中,這篇文章主要給大家介紹了關于vue項目npm?run?build打包dist文件及打包后空白的解決辦法,需要的朋友可以參考下
    2023-10-10
  • 使用Vue.js和MJML創(chuàng)建響應式電子郵件

    使用Vue.js和MJML創(chuàng)建響應式電子郵件

    這篇文章主要介紹了使用Vue.js和MJML創(chuàng)建響應式電子郵件,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-03-03
  • vue點擊按鈕實現(xiàn)讓頁面的某一個元素全屏展示

    vue點擊按鈕實現(xiàn)讓頁面的某一個元素全屏展示

    這篇文章主要介紹了vue點擊按鈕實現(xiàn)讓頁面的某一個元素全屏展示,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-10-10
  • web面試vue自定義組件及調用方式

    web面試vue自定義組件及調用方式

    這篇文章主要介紹了web面試中常問到的關于vue自定義組件及調用方式,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步
    2021-09-09
  • 使用antv替代Echarts實現(xiàn)數(shù)據(jù)可視化圖表詳解

    使用antv替代Echarts實現(xiàn)數(shù)據(jù)可視化圖表詳解

    這篇文章主要為大家介紹了使用antv替代Echarts實現(xiàn)數(shù)據(jù)可視化圖表詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-08-08
  • vue中引用阿里字體圖標的方法

    vue中引用阿里字體圖標的方法

    這篇文章主要介紹了vue中引用阿里字體圖標出現(xiàn)錯誤問題的解決方法,感興趣的朋友跟隨腳本之家小編一起學習吧
    2018-02-02
  • Element 的 el-table 表格實現(xiàn)單元格合并功能

    Element 的 el-table 表格實現(xiàn)單元格合并功能

    這篇文章主要介紹了Element 的 el-table 表格實現(xiàn)單元格合并功能,本文通過示例代碼給大家介紹的非常詳細,感興趣的朋友一起看看吧
    2024-07-07
  • Vue.js彈出模態(tài)框組件開發(fā)的示例代碼

    Vue.js彈出模態(tài)框組件開發(fā)的示例代碼

    本篇文章主要介紹了Vue.js彈出模態(tài)框組件開發(fā)的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-07-07
  • 一文詳解如何在vue中實現(xiàn)文件預覽功能

    一文詳解如何在vue中實現(xiàn)文件預覽功能

    很多Vue項目中都需要PDF文件預覽功能,比如合同ERP,銷售CRM,內部文檔CMS管理系統(tǒng),內置PDF文件在線預覽功能,下面這篇文章主要給大家介紹了關于如何在vue中實現(xiàn)文件預覽功能的相關資料,需要的朋友可以參考下
    2022-10-10
  • 關于vue-treeselect的基本用法

    關于vue-treeselect的基本用法

    vue-treeselect?是一個多選組件,具有對?Vue.js嵌套選項支持,這篇文章主要介紹了關于vue-treeselect的基本用法,需要的朋友可以參考下
    2022-11-11

最新評論