首页 > 其他分享 >根据滚动条到达该位置对应的导航选项卡高亮

根据滚动条到达该位置对应的导航选项卡高亮

时间:2023-05-24 16:11:59浏览次数:50  
标签:高亮 color top 滚动条 active nav var 选项卡 部分

html

<nav> <ul> <li><a href="#section1">第一部分</a></li> <li><a href="#section2">第二部分</a></li> <li><a href="#section3">第三部分</a></li> <li><a href="#section4">第四部分</a></li> </ul> </nav> <section id="section1"> <h2>第一部分</h2> <p>这是第一部分的内容</p> </section> <section id="section2"> <h2>第二部分</h2> <p>这是第二部分的内容</p> </section> <section id="section3"> <h2>第三部分</h2> <p>这是第三部分的内容</p> </section> <section id="section4"> <h2>第四部分</h2> <p>这是第四部分的内容</p> </section>

 

css

nav {
position: fixed;
top: 0;
left: 0;
width: 100%;
background-color: #fff;
z-index: 999;
}

nav ul {
list-style: none;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
}

nav li {
margin: 0 10px;
}

nav a {
display: block;
padding: 10px;
text-decoration: none;
color: #333;
}

nav a.active {
color: #f00;
}

 

section {
height: 500px;
margin-top: 50px;
padding: 20px;
background-color: #eee;
}

js

$(document).ready(function() {
var nav_height = $('nav').outerHeight(); // 获取导航栏高度
var sections = $('section'); // 获取所有章节
var nav_links = $('nav a'); // 获取所有导航链接

$(window).scroll(function() {
var current_pos = $(this).scrollTop(); // 获取当前滚动条位置

sections.each(function() {
var top = $(this).offset().top - nav_height - 20; // 获取章节顶部位置
var bottom = top + $(this).outerHeight(); // 获取章节底部位置

if (current_pos >= top && current_pos <= bottom) {
nav_links.removeClass('active'); // 移除所有导航链接的 active 类
$('nav a[href="#' + $(this).attr('id') + '"]').addClass('active'); // 添加当前导航链接的 active 类
}
});
});
});

标签:高亮,color,top,滚动条,active,nav,var,选项卡,部分
From: https://www.cnblogs.com/yangcaicai/p/17428620.html

相关文章

  • Element 表格固定列横向滚动条无法拖动的问题解决
    在Element-UI中,当对表格列进行固定后,底部的横向滚动条就无法拖动了,主要的问题就是固定区域盖住了横向滚动条。方案一:修改el-table__body-wrapper样式的层级,随便设个层级就可::v-deep.el-table__body-wrapper{z-index:2}//加了fixed之后失效::v-deep.el-table__fi......
  • WPF实现两个DataGrid列表的滚动条同步
    实现目标:左右两个DataGrid对比显示,希望拖动一个列表的滚动条,就把别一个列表的滚动条移动到相应位置。 主要思路是:通过FindVisualChildren找到两个DataGrid的ScrollViewer控件,然后注册两个控件的ScrollChanged事件,只要有一个ScrollViewer的VerticalOffset值变了,就相应地修改另......
  • 18-关键字高亮
    高亮原理我们在百度,京东搜索时,关键字会变成红色,比较醒目,这叫高亮显示:高亮显示的实现分为两步:1)给文档中的所有关键字都添加一个标签,例如<em>标签2)页面给<em>标签编写CSS样式实现高亮语法注意高亮是对关键字高亮,因此搜索条件必须带有关键字,而不能是范围这样的查询。......
  • 【element-plus】Table表格横向滚动条显示不正确解决办法
    滚动条bug展示如下:造成bug原因排查:由于对Table表格进行了二次封装,传递props属性时,传递了align属性,代码实现如下图:解决办法删除align属性,滚动条样式正常回显在二次封装组件时,需要保证组件属性的输入和透出,尽量避免不必要的bug......
  • css解决浏览器滚动条出现时页面宽度会缩窄页面抖动
    css解决浏览器滚动条出现时页面宽度会缩窄页面抖动解决方式如下方式一html{overflow-y:scroll;}总是显示滚动条,体验不好方式二html{overflow-y:overlay;}兼容性一般方式三html{margin-right:calc(100%-100vw);}方式四html{padding-......
  • overflow 出现滚动条 内容无法铺满
      如图:背景色是没有完全铺满的,现在使用的属性是:overflow:scroll改为overflow-y:overlay;即可......
  • Chrome浏览器好用的插件(批量保存所有选项卡网址、批量打开粘贴板网址插件)
    转自:https://www.cnblogs.com/nmap/p/6557883.html 1、Chrome批量保存所有选项卡网址+批量打开复制网址小插件批量保存所有选项卡网址插件:CopyAllUrls经常搜索一些东西,下班时无法处理完所有网页内容,比如做笔记什么的,又不舍得关闭网页。每个页面关闭前复制链接保存太麻烦......
  • DIV 随着滚动条 移动
    <!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><htmlxmlns="http://www.w3.org/1999/xhtml"><HEAD><TITLE>codeby:haiw......
  • 滚动条样式自定义
    ::-webkit-scrollbar-track-piece{  width:1px;  height:1px;  background-color:#0c1e26;}::-webkit-scrollbar{  width:1px;  height:1px;  background-color:#16d0ff;}::-webkit-scrollbar-thumb{  width:1px;  heig......
  • JavaFx 关键字高亮文本实现
    原文地址:JavaFx关键字高亮文本实现-Stars-One的杂货小窝整蓝奏云批量下载器里的搜索功能想到的一个关键字高亮功能,借助textflow组件来实现,记录一下本文基于TornadoFx框架进行编写,封装工具代码是kotlin版本然后也是顺便把这个封装成了stars-one/common-controls里的xHig......