我的网站只有一页,当它达到某个范围时,我正在尝试更改boostrap导航栏上的活动类。为了匹配固定的导航栏,我放置了跨度锚点。
这是我的HTML代码,如果有人可以使用有效的Jquery给我一个脚本,我将非常感谢。
<!doctype html>
<html lang=fr>
<head>
<meta charset="UTF-8">
<title>Home</title>
<link rel="stylesheet" href="style.css">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
</head>
<body>
<div class="navbar">
<nav class="navbar fixed-top navbar-expand-lg navbar-light bg-light">
<form class="form-inline">
<a class="navbar-brand" href="#">
<img id="logonavbar" src="images/logo_woman.png" width="100" height="100" alt="BB">
</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-item nav-link active" href="#">Home<span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-item nav-link" href="#group">Group</a>
</li>
<li class="nav-item">
<a class="nav-item nav-link" href="#musics">Musics</a>
</li>
<li>
<a class="nav-item nav-link" href="#social">Social feed</a>
</li>
<li>
<a class="nav-item nav-link" href="#news">News</a>
</li>
</ul>
</div>
</form>
</nav>
</div>
<div id="content">
<!--Anchor for fixed navbar-->
<span class="anchor" id="group"></span>
<div class="contentblock1" id="groupContent">
<!--My content-->
</div>
<span class="anchor" id="musics"></span>
<div class="contentblock2" id="musicsContent">
<!--My content-->
</div>
<span class="anchor" id="social"></span>
<div class="contentblock2" id="socialContent">
<!--My content-->
</div>
<span class="anchor" id="news"></span>
<div class="contentblock2" id="newsContent">
<!--My content-->
</div>
</div>
<!--JS sources-->
<script src="https://code.jquery.com/jquery-3.3.1.js" integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60=" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
<script>
var topMenu = $(".navbar-nav"),
topMenuHeight = topMenu.outerHeight()+15,
// All list items
menuItems = topMenu.find("li a"),
// Anchors corresponding to menu items
scrollItems = menuItems.map(function(){
var item = $($(this).attr("href"));
if (item.length) { return item; }
});
// Bind to scroll
$(window).scroll(function(){
// Get container scroll position
var fromTop = $(this).scrollTop()+topMenuHeight;
// Get id of current scroll item
var cur = scrollItems.map(function(){
if ($(this).offset().top < fromTop)
return this;
});
// Get the id of the current element
cur = cur[cur.length-1];
var id = cur && cur.length ? cur[0].id : "";
// Set/remove active class
menuItems
.parent().removeClass("active")
.end().filter("[href='#"+id+"']").parent().addClass("active");
});
</script>
</body>
</html>
非常感谢