mpvue实现左侧导航与右侧内容的联动


Posted in Javascript onOctober 21, 2019

本文实例为大家分享了mpvue左侧导航与右侧内容联动的具体代码,供大家参考,具体内容如下

效果图如下:

mpvue实现左侧导航与右侧内容的联动

(1)左侧导航联动右侧内容

实现:点击左侧导航,右侧内容滑到对应的位置,并且导航上有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)在左侧导航联动右侧内容的基础上增加右侧内容联动左侧导航

实现:滑动右侧内容区域,给左侧对应导航增加current样式,并且当导航高度过长,会联动其滚动

补充: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,用来联动内容区域
 currentIndex: 0,
 navulHeight: 0, // 导航里ul高度
 navItemHeight: 0, // 每个导航高度
 listHeight: [], // foods内部块的高度
 contentHeight: [], // 内容区域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组件学习教程进行学习。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
javascript 自定义事件初探
Aug 21 Javascript
获取焦点时,利用js定时器设定时间执行动作
Apr 02 Javascript
suggestion开发小结以及对键盘事件的总结(针对中文输入法状态)
Dec 20 Javascript
JS中图片缓冲loading技术的实例代码
Aug 29 Javascript
JS与jQuery遍历Table所有单元格内容的方法
Dec 07 Javascript
JS获取本周周一,周末及获取任意时间的周一周末功能示例
Feb 09 Javascript
JS实现静态页面搜索并高亮显示功能完整示例
Sep 19 Javascript
手把手教你用Node.js爬虫爬取网站数据的方法
Jul 05 Javascript
vue项目中,main.js,App.vue,index.html的调用方法
Sep 20 Javascript
java实现单链表增删改查的实例代码详解
Aug 30 Javascript
深入了解JavaScript 防抖和节流
Sep 12 Javascript
Vue移动端用淘宝弹性布局lib-flexible插件做适配的方法
May 26 Javascript
vue项目中常见问题及解决方案(推荐)
Oct 21 #Javascript
vue.js实现左边导航切换右边内容
Oct 21 #Javascript
vue+element树组件 实现树懒加载的过程详解
Oct 21 #Javascript
JavaScript函数IIFE使用详解
Oct 21 #Javascript
vue实现侧边栏导航效果
Oct 21 #Javascript
vue实现吸顶、锚点和滚动高亮按钮效果
Oct 21 #Javascript
vue-cli基础配置及webpack配置修改的完整步骤
Oct 20 #Javascript
You might like
PHP static局部静态变量和全局静态变量总结
2014/03/02 PHP
TP3.2框架分页相关实现方法分析
2020/06/03 PHP
List Installed Software Features
2007/06/11 Javascript
extjs 初始化checkboxgroup值的代码
2011/09/21 Javascript
javascript题目,重写函数让其无限相加
2012/02/15 Javascript
jQuery获取样式中的背景颜色属性值/颜色值
2012/12/17 Javascript
用javascript添加控件自定义属性解析
2013/11/25 Javascript
jQuery的one()方法用法实例
2015/01/19 Javascript
JavaScript中字符串分割函数split用法实例
2015/04/07 Javascript
微信小程序 绘图之饼图实现
2016/10/24 Javascript
AjaxUpLoad.js实现文件上传功能
2018/03/02 Javascript
Angular4 Select选择改变事件的方法
2018/10/09 Javascript
layer.prompt输入层的例子
2019/09/24 Javascript
vue实现在线翻译功能
2019/09/27 Javascript
vuecli3.x中轻松4步带你使用tinymce的步骤
2020/06/25 Javascript
原生js实现俄罗斯方块
2020/10/20 Javascript
[04:29]【TI9采访】OG.N0tail在胜者组决赛后接受采访
2019/08/25 DOTA
[01:11:46]DOTA2-DPC中国联赛 正赛 iG vs Magma BO3 第一场 2月23日
2021/03/11 DOTA
Python Scapy随心所欲研究TCP协议栈
2018/11/20 Python
python按顺序重命名文件并分类转移到各个文件夹中的实现代码
2020/07/21 Python
纯css3实现的竖形无限级导航
2014/12/10 HTML / CSS
Canvas中设置width与height的问题浅析
2018/11/01 HTML / CSS
关于html字符串正则判断和匹配的具体使用
2019/12/12 HTML / CSS
美国最大网上鞋店:Zappos
2016/07/25 全球购物
您的健身减肥和健康饮食专家:vitafy
2017/06/06 全球购物
Boston Proper官网:美国女装品牌
2017/10/30 全球购物
万户网络JAVA程序员岗位招聘笔试试卷
2013/01/08 面试题
求职信内容考虑哪几点
2013/10/05 职场文书
安全大检查反思材料
2014/01/31 职场文书
学校万圣节活动方案
2014/02/13 职场文书
2014年学习全国道德模范事迹思想汇报
2014/09/15 职场文书
后进基层党组织整改方案
2014/10/25 职场文书
2014年质量工作总结
2014/11/22 职场文书
gateway与spring-boot-starter-web冲突问题的解决
2021/07/16 Java/Android
Java tomcat手动配置servlet详解
2021/11/27 Java/Android
Nebula Graph解决风控业务实践
2022/03/31 MySQL