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

实现锚点链接点击tab跳转到指定位置 并且滚动鼠标顶部锚点的样式也跟随变化

实现效果如下  不管是点击还是  滚动鼠标 顶部的样式也会跟随变化

点击会跳转到指定的位置 

通过IntersectionObserver 监听是否可见 

下面代码可以直接执行到vue的文件 

<template><div><ul class="nav"><li v-for="tab in tabs" :key="tab.name" :class="{ active: currentTab === tab.name }" @click="scrollToTab(tab)">{{ tab.label }}</li><div class="underline" :style="underlineStyle"></div></ul><div class="section" id="section1">Section 1</div><div class="section" id="section2">Section 2</div><div class="section" id="section3">Section 3</div><div class="section" id="section4">Section 4</div><div class="section" id="section5">Section 5</div></div>
</template><script>
export default {data () {return {currentTab: 'tab1',tabs: [{ name: 'tab1', label: '首页', id: 'section1' },{ name: 'tab2', label: '服务内容', id: 'section2' },{ name: 'tab3', label: '业务案例', id: 'section3' },{ name: 'tab4', label: '关于我们', id: 'section4' },{ name: 'tab5', label: '联系方式', id: 'section5' }],underlineStyle: {width: '0px',left: '0px'}}},methods: {scrollToTab (section) {this.currentTab = section.namethis.updateUnderline()const element = document.getElementById(section.id)document.getElementById(section.id).scrollIntoView({ behavior: 'smooth', block: 'center' })},updateUnderline () {this.$nextTick(() => {const activeTab = this.$el.querySelector('.nav .active')if (activeTab) {this.underlineStyle.width = `${activeTab.offsetWidth}px`this.underlineStyle.left = `${activeTab.offsetLeft}px`}})},handleIntersection (entries) {entries.forEach((entry) => {if (entry.isIntersecting) {const tab = this.tabs.find((tab) => tab.id === entry.target.id)if (tab) {this.currentTab = tab.namethis.updateUnderline()}}})}},mounted () {this.updateUnderline()const options = {root: null,rootMargin: `-${this.$el.querySelector('.nav').offsetHeight}px 0px 0px 0px`,threshold: 0.5}const observer = new IntersectionObserver(this.handleIntersection, options)this.tabs.forEach((tab) => {const section = document.getElementById(tab.id)if (section) {observer.observe(section)}})window.addEventListener('resize', this.updateUnderline)},beforeDestroy () {window.removeEventListener('resize', this.updateUnderline)}
}
</script><style scoped>
.nav {display: flex;position: fixed;top: 0;width: 1000px;background-color: white;z-index: 1000;border-bottom: 1px solid #ccc;
}
.nav li {flex: 1;text-align: center;padding: 10px;cursor: pointer;position: relative;
}
.nav li.active {color: blue;
}
.underline {position: absolute;bottom: 0;height: 2px;background-color: blue;transition: width 0.3s, left 0.3s;
}
.section {height: 300px;padding-top: 60px; /* 留出导航栏的高度 */border-bottom: 1px solid #ccc;
}
</style>

相关文章:

  • 了解 XML HttpRequest 及其在 Web 开发中的应用
  • 什么是模型轻量化?如何自动进行模型轻量化?
  • Linux命令的语法和帮助系统
  • 海豚调度异常处理: 使用 arthas 在内存中删除启动失败的工作流
  • AMS(ActivityManagerService)源码解析2,Android应用是如何被启动的
  • 【Android】安Android Studio环境搭建注意点
  • QT
  • 练习时长 1 年 2 个月的 Java 菜鸡练习生最近面经,期望25K
  • article:文章信息表
  • 和鲸101计划:以神经计算建模培训,助力北大学术人才培养
  • lspci总结
  • 【最新鸿蒙应开发】——HarmonyOS沙箱目录
  • 【Kafka】Kafka Producer 分区-05
  • Python 使用 Thick 方式连接 Oracle Database BaseDB 23ai
  • 前端将Markdown文本转换为富文本显示/编辑,并保存为word文件
  • Android 初级面试者拾遗(前台界面篇)之 Activity 和 Fragment
  • Date型的使用
  • javascript数组去重/查找/插入/删除
  • Laravel Mix运行时关于es2015报错解决方案
  • LeetCode刷题——29. Divide Two Integers(Part 1靠自己)
  • Mysql数据库的条件查询语句
  • Selenium实战教程系列(二)---元素定位
  • 从地狱到天堂,Node 回调向 async/await 转变
  • 搭建gitbook 和 访问权限认证
  • 对话:中国为什么有前途/ 写给中国的经济学
  • 工程优化暨babel升级小记
  • 爬虫进阶 -- 神级程序员:让你的爬虫就像人类的用户行为!
  • 删除表内多余的重复数据
  • 手机端车牌号码键盘的vue组件
  • 说说动画卡顿的解决方案
  • 微信开放平台全网发布【失败】的几点排查方法
  • mysql 慢查询分析工具:pt-query-digest 在mac 上的安装使用 ...
  • # 睡眠3秒_床上这样睡觉的人,睡眠质量多半不好
  • #微信小程序:微信小程序常见的配置传值
  • (10)工业界推荐系统-小红书推荐场景及内部实践【排序模型的特征】
  • (4)(4.6) Triducer
  • (JSP)EL——优化登录界面,获取对象,获取数据
  • (算法)Travel Information Center
  • (提供数据集下载)基于大语言模型LangChain与ChatGLM3-6B本地知识库调优:数据集优化、参数调整、Prompt提示词优化实战
  • (已解决)什么是vue导航守卫
  • (转) ns2/nam与nam实现相关的文件
  • (转)socket Aio demo
  • (自适应手机端)响应式新闻博客知识类pbootcms网站模板 自媒体运营博客网站源码下载
  • .NET CORE使用Redis分布式锁续命(续期)问题
  • .NET 同步与异步 之 原子操作和自旋锁(Interlocked、SpinLock)(九)
  • .Net调用Java编写的WebServices返回值为Null的解决方法(SoapUI工具测试有返回值)
  • .net经典笔试题
  • .Net下C#针对Excel开发控件汇总(ClosedXML,EPPlus,NPOI)
  • @SuppressWarnings(unchecked)代码的作用
  • @Transactional 竟也能解决分布式事务?
  • [ C++ ] STL---仿函数与priority_queue
  • [ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname
  • [ vulhub漏洞复现篇 ] ThinkPHP 5.0.23-Rce
  • [C/C++]关于C++11中的std::move和std::forward
  • [C++]priority_queue的介绍及模拟实现