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 异步页面查询实现代码(asp.net)
May 26 Javascript
js 获取元素下面所有li的两种方法
Apr 14 Javascript
深入理解JavaScript系列(28):设计模式之工厂模式详解
Mar 03 Javascript
究竟什么是Node.js?Node.js有什么好处?
May 29 Javascript
jQuery学习笔记之入门
Dec 14 Javascript
JS获取本周周一,周末及获取任意时间的周一周末功能示例
Feb 09 Javascript
详解ES6中的代理模式——Proxy
Jan 08 Javascript
解决js ajax同步请求造成浏览器假死的问题
Jan 18 Javascript
微信小程序之swiper滑动面板用法示例
Dec 04 Javascript
JavaScript函数式编程(Functional Programming)组合函数(Composition)用法分析
May 22 Javascript
webpack常用配置总览(小结)
Nov 18 Javascript
比较node.js和Deno
Apr 27 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
ThinkPHP CURD方法之where方法详解
2014/06/18 PHP
PHP实现自动登入google play下载app report的方法
2014/09/23 PHP
TP5(thinkPHP5)框架使用ajax实现与后台数据交互的方法小结
2020/02/10 PHP
6款新颖的jQuery和CSS3进度条插件推荐
2013/03/05 Javascript
使用script的src实现跨域和类似ajax效果
2014/11/10 Javascript
详解JavaScript中循环控制语句的用法
2015/06/03 Javascript
js仿3366小游戏选字游戏
2016/04/14 Javascript
jQuery 监控键盘一段时间没输入
2016/04/22 Javascript
React中jquery引用的实现方法
2017/09/12 jQuery
解决vue中无法动态修改jqgrid组件 url地址的问题
2018/03/01 Javascript
微信小程序项目总结之点赞 删除列表 分享功能
2018/06/25 Javascript
Vue使用localStorage存储数据的方法
2019/05/27 Javascript
node express使用HTML模板的方法示例
2019/08/22 Javascript
vue 使用外部JS与调用原生API操作示例
2019/12/02 Javascript
vue-cli点击实现全屏功能
2020/03/07 Javascript
vue双击事件2.0事件监听(点击-双击-鼠标事件)和事件修饰符操作
2020/07/27 Javascript
vue实践---根据不同环境,自动转换请求的url地址操作
2020/09/21 Javascript
vue+elementUI实现简单日历功能
2020/09/24 Javascript
python 生成目录树及显示文件大小的代码
2009/07/23 Python
布同 统计英文单词的个数的python代码
2011/03/13 Python
python爬虫爬取某站上海租房图片
2018/02/04 Python
Python数据持久化shelve模块用法分析
2018/06/29 Python
python打包生成的exe文件运行时提示缺少模块的解决方法
2018/10/31 Python
利用Python将数值型特征进行离散化操作的方法
2018/11/06 Python
python开发实例之python使用Websocket库开发简单聊天工具实例详解(python+Websocket+JS)
2020/03/18 Python
构建高效的python requests长连接池详解
2020/05/02 Python
Python发送邮件封装实现过程详解
2020/05/09 Python
Pycharm如何自动生成头文件注释
2020/11/14 Python
用React加CSS3实现微信拆红包动画效果
2017/03/13 HTML / CSS
video实现有声音自动播放的实现方法
2020/05/20 HTML / CSS
杭州时比特电子有限公司SQL
2013/08/22 面试题
八一演出活动方案
2014/02/03 职场文书
关爱女孩行动实施方案
2014/03/13 职场文书
建设幸福中国演讲稿
2014/09/11 职场文书
2014年中学生检讨书大全
2014/10/09 职场文书
消防验收申请报告
2015/05/15 职场文书