当前位置: 首页 > news >正文

vue点击导航滚动到相应位置,鼠标滚动到相应位置对应导航名称高亮

html:

<template><div style="display: flex;justify-content: space-between;align-items: center;"><!-- 左侧是滚动内容 --><div style="width: 80%"><!-- 每个card都有对应的id --><el-card style="margin-bottom: 6px;" id="section-1"><div slot="header" style="font-size: 20px;font-weight: 700;display: flex;"><div style="background-color: #409eff;width: 4px;height: 20px; border-radius: 2px;margin-left: -21px;margin-right: 10px;"></div><div>综合评价1</div></div><div>内容一</div></el-card><el-card style="margin-bottom: 6px;" id="section-2"><div slot="header" style="font-size: 20px;font-weight: 700;display: flex;"><div style="background-color: #409eff;width: 4px;height: 20px; border-radius: 2px;margin-left: -21px;margin-right: 10px;"></div><div>综合评价2</div></div><div>内容二</div></el-card><el-card style="margin-bottom: 6px;" id="section-3"><div slot="header" style="font-size: 20px;font-weight: 700;display: flex;"><div style="background-color: #409eff;width: 4px;height: 20px; border-radius: 2px;margin-left: -21px;margin-right: 10px;"></div><div>综合评价3</div></div><div>内容三</div></el-card><el-card style="margin-bottom: 6px;" id="section-4"><div slot="header" style="font-size: 20px;font-weight: 700;display: flex;"><div style="background-color: #409eff;width: 4px;height: 20px; border-radius: 2px;margin-left: -21px;margin-right: 10px;"></div><div>综合评价4</div></div><div>内容四</div></el-card><el-card style="margin-bottom: 6px;" id="section-5"><div slot="header" style="font-size: 20px;font-weight: 700;display: flex;"><div style="background-color: #409eff;width: 4px;height: 20px; border-radius: 2px;margin-left: -21px;margin-right: 10px;"></div><div>综合评价5</div></div><div>内容五</div></el-card></div><!-- 右侧是导航栏,用elementui的时间线组件 el-timeline-item 来展示 --><!-- 导航分为父级和子级两层,一个父级包含多个子级,可以参考 navItems 数据格式 --><!--  :hide-timestamp="item.title === '回到顶部'" ————只有回到顶部 才隐藏时间戳 --><!--  :color="currentSectionParent === index ? item.color : ''" ————给选中的导航节点添加颜色展示 --><!-- :class="{'activeNav': currentSection == obj.id}"  ————给当前点击的导航名称添加颜色展示 --><div style="width: 20%"><div style="width: 11%" class="rightNav"><el-timeline><el-timeline-item v-for="(item, index) in navItems" :key="index" :timestamp="item.title" :hide-timestamp="item.title === '回到顶部'":color="currentSectionParent === index ? item.color : ''"placement="top"  ><div v-for="(obj,idx) in item.children" :key="idx" @click="scrollTo(obj,idx,index)"class="navTtem":class="{'activeNav': currentSection == obj.id}">{{ obj.title }}</div></el-timeline-item></el-timeline></div></div></div></template>

js:

data () {return {currentSection: 'section-1', // 默认高亮第一个currentSectionParent: 0,navItems: [{title: '导航1', color: '#409eff',children: [{title: '导航1-1', id: 'section-1'}]},{title: '导航2',color: '#409eff',children: [{title: '导航2-1', id: 'section-2'},{title: '导航2-2', id: 'section-3'},{title: '导航2-2', id: 'section-4'},]},{title: '导航3',color: '#409eff',children: [{title: '导航3-1', id: 'section-5'}]},{title: '回到顶部',children: [{title: '回到顶部', id: 'section-6'}]}]}
}
// mounted 和 beforeDestroy 生命周期钩子:分别在组件挂载时和销毁前添加和移除滚动事件监听器
mounted () {window.addEventListener('scroll', this.handleScroll);
},
beforeDestroy() {window.removeEventListener('scroll', this.handleScroll);
},
methods: {// 点击导航时,使用 scrollIntoView 方法平滑滚动到相应位置,并更新 currentSection 和 currentSectionParent 的值来高亮导航项scrollTo (obj,idx,index) {// 回到顶部if (obj.id === 'section-6') {window.scrollTo({ top: 0, behavior: 'smooth' });return}this.currentSection = obj.idthis.currentSectionParent = indexconst el = document.getElementById(obj.id);if (el) {el.scrollIntoView({ behavior: 'smooth' });}},// handleScroll 方法:监听滚动事件,计算当前滚动的位置,根据滚动位置更新 currentSection 和 currentSectionParent handleScroll() {const scrollPosition = window.scrollY;this.navItems.forEach((item,index) => {item.children.forEach((obj) => {const element = document.getElementById(obj.id);if (element) {const elementTop = element.offsetTop;const elementBottom = elementTop + element.clientHeight;if (scrollPosition >= elementTop && scrollPosition < elementBottom) {this.currentSection = obj.id;this.currentSectionParent = index}}})});},
},

css:

<style lang="scss" scoped>
::v-deep .el-timeline {padding-left: 15px;
}
::v-deep .el-timeline-item {padding-bottom: 0px;
}
::v-deep .el-timeline-item__timestamp.is-top {margin-bottom: 5px;
}
::v-deep .el-timeline-item__wrapper {padding-left: 20px;
}.rightNav {position: fixed;top: 50%;right: 10px;
}
.navTtem {padding: 3px 0;font-size: 12px;cursor: pointer;
}
.activeNav{color: #409eff;
}
</style>

效果图(数据对不上,但是展示效果是一样的)
![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/03c89a119a1f4100915664957c8c5069.png
在这里插入图片描述

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • Golang | Leetcode Golang题解之第390题消除游戏
  • 一款支持身份证、驾驶证、护照、车牌等证件识别插件
  • 【iOS】属性关键字
  • 爬取知乎回答
  • Unity(2022.3.41LTS) - 着色器
  • vulnhub靶场-DC2
  • 使用Blender云渲染的好处是什么?
  • 滚雪球学MyBatis-Plus(04):基础配置
  • 如何用Java SpringBoot打造助农捐赠平台?2025年25届毕业生必看+最新设计实现攻略!
  • Spring Cloud Gateway整合基于STOMP协议的WebSocket实战及遇到问题解决
  • Catia二次开发“CAAV5-ERROR: unauthorized API for CAA V5”错误
  • Docker 修改镜像源
  • 公司电脑的敏感文件怎么审查?分为六步,步步为营,保护文件不泄密
  • 联蔚盘云亮相CDIE消费品行业峰会
  • LABVIEW数据保存文件
  • [ JavaScript ] 数据结构与算法 —— 链表
  • 【跃迁之路】【444天】程序员高效学习方法论探索系列(实验阶段201-2018.04.25)...
  • Django 博客开发教程 16 - 统计文章阅读量
  • fetch 从初识到应用
  • HTTP请求重发
  • Java IO学习笔记一
  • JavaSE小实践1:Java爬取斗图网站的所有表情包
  • mysql innodb 索引使用指南
  • MySQL主从复制读写分离及奇怪的问题
  • PhantomJS 安装
  • Redux系列x:源码分析
  • windows下使用nginx调试简介
  • 爱情 北京女病人
  • 安卓应用性能调试和优化经验分享
  • 半理解系列--Promise的进化史
  • - 概述 - 《设计模式(极简c++版)》
  • 官方解决所有 npm 全局安装权限问题
  • 回顾 Swift 多平台移植进度 #2
  • 手机app有了短信验证码还有没必要有图片验证码?
  • UI设计初学者应该如何入门?
  • 长三角G60科创走廊智能驾驶产业联盟揭牌成立,近80家企业助力智能驾驶行业发展 ...
  • 整理一些计算机基础知识!
  • #define用法
  • (C语言版)链表(三)——实现双向链表创建、删除、插入、释放内存等简单操作...
  • (七)Knockout 创建自定义绑定
  • (三) diretfbrc详解
  • (生成器)yield与(迭代器)generator
  • (一)80c52学习之旅-起始篇
  • (一)使用Mybatis实现在student数据库中插入一个学生信息
  • (转)拼包函数及网络封包的异常处理(含代码)
  • (转)全文检索技术学习(三)——Lucene支持中文分词
  • (转)总结使用Unity 3D优化游戏运行性能的经验
  • *Algs4-1.5.25随机网格的倍率测试-(未读懂题)
  • .NET CF命令行调试器MDbg入门(四) Attaching to Processes
  • .net dataexcel 脚本公式 函数源码
  • .NET 服务 ServiceController
  • .net 无限分类
  • .NET/C# 反射的的性能数据,以及高性能开发建议(反射获取 Attribute 和反射调用方法)
  • .net6 当连接用户的shell断掉后,dotnet会自动关闭,达不到长期运行的效果。.NET 进程守护
  • .Net多线程Threading相关详解