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 相关文章推荐
Mootools 1.2教程 设置和获取样式表属性
Sep 15 Javascript
jquery 提交值不为空的元素示例代码
May 10 Javascript
IE下JS读取xml文件示例代码
Aug 05 Javascript
Jquery实现兼容各大浏览器的Enter回车切换输入焦点的方法
Sep 01 Javascript
jQuery Tags Input Plugin(添加/删除标签插件)详解
Jun 20 Javascript
jQuery实现的自定义滚动条实例详解
Sep 20 Javascript
JavaScript基本类型值-Undefined、Null、Boolean
Feb 23 Javascript
微信小程序开发教程之增加mixin扩展
Aug 09 Javascript
vue .js绑定checkbox并获取、改变选中状态的实例
Aug 24 Javascript
jQuery实现模拟搜索引擎的智能提示功能简单示例
Jan 27 jQuery
TypeScript中的方法重载详解
Apr 12 Javascript
js实现打字小游戏
Dec 17 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入门
2006/10/09 PHP
php数组函数序列之prev() - 移动数组内部指针到上一个元素的位置,并返回该元素值
2011/10/31 PHP
php中并发读写文件冲突的解决方案
2013/10/25 PHP
yii2中关于加密解密的那些事儿
2018/06/12 PHP
Laravel框架控制器,视图及模型操作图文详解
2019/12/04 PHP
PDO实现学生管理系统
2020/03/21 PHP
jquery 框架使用教程 AJAX篇
2009/10/11 Javascript
JSON 和 JavaScript eval使用说明
2010/06/13 Javascript
悄悄用脚本检查你访问过哪些网站的代码
2010/12/04 Javascript
jQuery源码分析-03构造jQuery对象-工具函数
2011/11/14 Javascript
javascript针对DOM的应用分析(四)
2012/04/15 Javascript
javascript中style.left和offsetLeft的用法说明
2014/03/07 Javascript
jquery使用slideDown实现模块缓慢拉出效果的方法
2015/03/27 Javascript
JS实现的鼠标跟随代码(卡通手型点击效果)
2015/10/26 Javascript
Angularjs的Controller间通信机制实例分析
2016/11/07 Javascript
bootstrap table操作技巧分享
2017/02/15 Javascript
使用BootStrap实现标签切换原理解析
2017/03/14 Javascript
Vue上传组件vue Simple Uploader的用法示例
2017/08/25 Javascript
angularJS1 url中携带参数的获取方法
2018/10/09 Javascript
Vue注册组件命名时不能用大写的原因浅析
2019/04/25 Javascript
vue 中的 render 函数作用详解
2020/02/28 Javascript
jQuery实现简单评论功能
2020/08/19 jQuery
vue 虚拟DOM的原理
2020/10/03 Javascript
实用的 vue tags 创建缓存导航的过程实现
2020/12/03 Vue.js
使用js获取身份证年龄的示例代码
2020/12/11 Javascript
Python中文分词工具之结巴分词用法实例总结【经典案例】
2017/04/15 Python
python框架Django实战商城项目之工程搭建过程图文详解
2020/03/09 Python
jupyter 实现notebook中显示完整的行和列
2020/04/09 Python
英国太阳镜品牌:Taylor Morris Eyewear
2018/04/18 全球购物
警示教育观后感
2015/06/17 职场文书
学术研讨会主持词
2015/07/04 职场文书
重阳节活动主持词
2015/07/04 职场文书
2016党风廉政建设心得体会范文
2016/01/25 职场文书
导游词之新疆尼雅遗址
2019/10/16 职场文书
利用Python判断整数是否是回文数的3种方法总结
2021/07/07 Python
Go语言怎么使用变长参数函数
2022/07/15 Golang