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

VUE篇之,实现锚点定位,滚动与导航联动

实现效果:点击侧边栏,右侧内容能够自动滑动到显示区域

滚动显示区域,侧边栏能够跟随高亮

代码如下:

<template><div class="root"><div class="slidebar"><ul class="sidebar-anchor-list"><liclass="sidebar-anchor-item":class="{ active: item.anchor === anchor }"v-for="item in anchorList"@click="anchorPosition(item.anchor)"><a class="sidebar-anchor-item-link"><span class="sidebar-anchor-item-text">{{ item.text }}</span></a></li></ul></div><div class="main" id="main"><div class="page-container" v-for="item in anchorList" :key="item.anchor" :id="item.anchor.replace(/#/g, '')">{{ item.text }}</div></div></div>
</template>
<script>
import _ from 'lodash';
export default {data() {return {anchorList: [{anchor: '#labs',text: '实验室',},{anchor: '#courses',text: '学好课',},{anchor: '#project',text: '好作品',},{anchor: '#codingnews',text: '资讯',},{anchor: '#cocase',text: '校园',},{anchor: '#cooperator',text: '合作',},],anchor: '#labs',offsetTopList: [],};},mounted() {this.$nextTick(() => {this.setOffsetTopList();document.getElementById('main').addEventListener('scroll', _.debounce(this.scrollHandle.bind(this), 20));});},methods: {anchorPosition(val) {this.anchor = val;this.scrollTo(val);},//获取每个模块距离顶部的距离setOffsetTopList() {const mainEl = document.querySelector('#main');this.offsetTopList = this.anchorList.map((item, index) => {const element = document.querySelector(item.anchor);return {offsetTop: index === 0 ? mainEl.offsetTop : element.offsetTop,anchor: item.anchor,};});console.log(this.offsetTopList);},scrollHandle({ target }) {const curScrollTop = target.scrollTop;const len = this.offsetTopList.length;for (let i = len - 1; i >= 0; i--) {const curReference = this.offsetTopList[i].offsetTop; // 当前参考值if (curScrollTop >= curReference - 10) {this.anchor = this.offsetTopList[i].anchor;break;}}},scrollTo(anchor) {const element = document.querySelector(anchor);const parent = document.querySelector('#main');parent.scrollTo({top: element.offsetTop,behavior: 'smooth',});},},
};
</script>
<style lang="scss" scoped>
* {list-style: none;padding: 0;margin: 0;
}
.root {width: 100%;height: 100%;display: flex;
}
.slidebar {width: 200px;height: fit-content;background-color: #f0f0f0;margin-top: 30px;.sidebar-anchor-item {height: 68px;border-bottom: 1px solid #eff3f8;display: flex;align-items: center;justify-content: center;text-align: center;font-size: 12px;transition: all 200ms;.sidebar-anchor-item-link {display: block;width: 100%;color: rgba(74, 89, 111, 0.6);.sidebaricon {display: block;margin: 0 auto 6px;font-size: 18px;}.sidebar-anchor-item-text {display: block;}}&:hover {.sidebar-anchor-item-link {color: #009cff;}}&.active {.sidebar-anchor-item-link {color: #009cff;}&:hover {.sidebar-anchor-item-link {color: #009cff;}}}}
}
.main {width: 100%;height: 100%;overflow: auto;.page-container {width: 100%;height: 500px;border-bottom: 1px solid pink;}
}
</style>

 

http://www.xdnf.cn/news/1195.html

相关文章:

  • 黑盒测试——等价类划分法实验
  • 虚拟机超详细Ubuntu安装教程
  • 测试基础笔记第九天
  • Idea创建项目的搭建
  • Git入门
  • 从 0 到 1 打通 AI 工作流:Dify+Zapier 实现工具自动化调用实战
  • 进阶篇 第 3 篇:经典永不落幕 - ARIMA 模型详解与实践
  • 乐视系列玩机---乐视2 x520 x528等系列线刷救砖以及刷写第三方twrp 卡刷第三方固件步骤解析
  • EAL4+与等保2.0:解读中国网络安全双标准
  • 【深度学习】LoRA:低秩适应性微调技术详解
  • 【数学建模】孤立森林算法:异常检测的高效利器
  • NDSS 2025|侧信道与可信计算攻击技术导读(二)系统化评估新旧缓存侧信道攻击技术
  • YOLO-E:详细信息
  • 【机器学习案列-21】基于 LightGBM 的智能手机用户行为分类
  • 2022年全国职业院校技能大赛 高职组 “大数据技术与应用” 赛项赛卷(10卷)任务书
  • jenkins pipeline ssh协议报错处理
  • 多模态模型实现原理详细介绍
  • Python 设计模式:模板模式
  • FastText 模型文本分类实验:从零到一的实战探索
  • 4.22tx视频后台开发一面
  • JAVA:Web安全防御
  • 考研系列-计算机网络-第五章、传输层
  • 什么是CRM系统,它的作用是什么?CRM全面指南
  • 信奥赛CSP-J复赛集训(DP专题)(19):P3399 丝绸之路
  • 基于51单片机的温度控制系统proteus仿真
  • 客户端 AI 与服务器端 AI 的深度比较及实践建议?
  • 精益数据分析(15/126):解锁数据分析关键方法,驱动业务增长
  • 【数字图像处理】立体视觉信息提取
  • 鸿蒙Flutter仓库停止更新?
  • 深度解析MySQL INSERT ... ON DUPLICATE KEY UPDATE语句