jquery - 滚动时导航未突出显示
问题描述
我创建了一个动态生成目录的脚本,但由于某种原因,它在滚动时没有突出显示。我不确定为什么它在滚动时不突出显示单个链接。下面是我的 jQuery 脚本。
<script>
$(document).ready(function() {
$("#toc").append(' ')
$("h2").each(function(i) {
var current = $(this);
current.attr("id", "title" + i);
$("#toc").append("<li>" + "<a id='link" + i + "' href='#title" + i + "' class='active'" + "' title='" + current.attr("tagName") + "'>" + current.html() + "</a>" + "</li>");
});
});
$(window).on('scroll', function() {
$('h2').each(function() {
if($(window).scrollTop() >= $(this).offset().top) {
var id = $(this).attr('id');
$('#toc toc a').removeClass('active');
$('#toc toc a[href=#'+ id +']').addClass('active');
}
});
});
</script>
这是我的 jsfiddle 的链接:https ://jsfiddle.net/dude12go8/138evyj0/4/
解决方案
对于 jQuery 中的属性选择器,您应该使用属性后跟$
. 查看文档Jquery 属性选择器
我还在以下块中进行了更改,请检查代码段。
$('#toc li a').removeClass('active');
$('a[href$="'+id+'"]').addClass('active');
还要$('li:first-child').children().addClass('active');
在末尾添加或.each()
将活动类添加到第一项。
var $ = jQuery;
$(document).ready(function() {
$("#toc").append(' ')
$("h2").each(function(i) {
var current = $(this);
current.attr("id", "title" + i);
$("#toc").append(`<li><a id="link${i}" href="#title${i}" title="${current.attr("tagName")}">${current.html()}</a></li>`);
});
$('li:first-child').children().addClass('active'); // Add this to add active class only on first item
});
$(window).on('scroll', function() {
$('h2').each(function() {
if($(window).scrollTop() >= $(this).offset().top) {
var id = $(this).attr('id');
$('#toc li a').removeClass('active');
$('a[href$="'+id+'"]').addClass('active');
}
});
});
h2 {
border-bottom: 1px dotted #000;
color: #000;
margin-top: 1.5rem;
}
#toc{
color: #000;
list-style: none;
margin: 0, 0, 0;
}
#toc li {
margin: 1rem 0.2rem 0;
color: #000;
}
#toc li a{
color: #000;
font-family: Calibri, Sans-sarif;
font-weight: bold;
font-size: 16px;
cursor: pointer;
text-decoration: none;
list-style: none;
}
#toc li a:hover, #toc a.active {
background: #666;
color: #000;
}
.nav-container{
position: fixed;
left: 0px;
width: 160px;
height: 180px;
padding-top: 0px;
}
.container{
margin-left: 120px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
<html>
<head>
<meta charset="UTF-8">
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<title>Untitled Document</title>
</head>
<body>
<nav class="nav-container">
<ul id="toc"></ul>
</nav>
<div class="container">
<h2>Test1</h2>
<p>This is a test</p>
<p>This is a test</p>
<h2>Test2</h2>
<p>TLSDGn lsjdlgjsldj gljlsdjgljlsdjgljdsl gjldfjgljdfljgldjflkgj ldfjgljldfjgl jldfg</p>
<p>This is a test</p>
<p>This is a another test</p>
<p>This is a another another another test</p>
<h2>Test3</h2>
<p>This is a test</p>
<p>This is a test</p>
<h2>Test4</h2>
<p>TLSDGn lsjdlgjsldj gljlsdjgljlsdjgljdsl gjldfjgljdfljgldjflkgj ldfjgljldfjgl jldfg</p>
<p>This is a test</p>
<p>This is a another test</p>
<p>This is a another another another test</p>
<h2>Test5</h2>
<p>This is a test</p>
<p>This is a test</p>
<h2>Test6</h2>
<p>TLSDGn lsjdlgjsldj gljlsdjgljlsdjgljdsl gjldfjgljdfljgldjflkgj ldfjgljldfjgl jldfg</p>
<p>This is a test</p>
<p>This is a another test</p>
<p>This is a another another another test</p>
</div>
</body>
</html>
推荐阅读
- azure-devops - 我在基于 git 的 wiki 更新程序任务中面临以下问题
- python - 如何使用两列进行熊猫分组,但合并组以获取这两列中键的唯一组合
- javascript - 如何在公共键中获取对象值数组
- python - 为什么我的 URL 没有激活 django 中的视图?
- angular - ngModel 有条件时读取值时出错
- python - 检查输入时出错:预期 conv1d_195_input 有 3 个维度,但得到了形状为 (1229, 1) 的数组
- java - maven 不从命令行检测测试
- sql - 有没有办法用不同的值更新同一个表中的多行?
- postgresql - Postgresql 在 COPY-TO 时向字符串添加双引号
- msal - Microsoft 单点登录 - react-aad-msal 库 - 无法获取访问令牌