jQuery滚动导航菜单是一种基于jQuery库实现的网页交互功能,它允许用户在滚动页面时,导航菜单能够自动高亮显示当前可视区域的相应内容。这种功能通常用于长页面,帮助用户快速定位和导航。
以下是一个简单的基于锚点的滚动导航菜单实现示例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>滚动导航菜单示例</title>
<style>
body {
font-family: Arial, sans-serif;
}
.nav {
position: fixed;
top: 0;
width: 100%;
background-color: #f1f1f1;
padding: 10px;
text-align: center;
}
.nav a {
color: black;
text-decoration: none;
margin: 0 15px;
}
.nav a.active {
color: red;
}
.section {
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
font-size: 24px;
}
</style>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<div class="nav">
<a href="#section1" class="active">Section 1</a>
<a href="#section2">Section 2</a>
<a href="#section3">Section 3</a>
</div>
<div id="section1" class="section">Section 1</div>
<div id="section2" class="section">Section 2</div>
<div id="section3" class="section">Section 3</div>
<script>
$(document).ready(function() {
$('a[href^="#"]').on('click', function(event) {
if (this.hash !== "") {
event.preventDefault();
var hash = this.hash;
$('html, body').animate({
scrollTop: $(hash).offset().top
}, 800, function() {
window.location.hash = hash;
});
}
});
$(window).scroll(function() {
var scroll = $(window).scrollTop();
$('.section').each(function() {
var offset = $(this).offset().top - 200;
var id = $(this).attr('id');
var height = $(this).height();
if (scroll > offset && scroll < offset + height) {
$('.nav a').removeClass('active');
$('.nav').find('a[href="#' + id + '"]').addClass('active');
}
});
});
});
</script>
</body>
</html>
通过以上示例代码和常见问题解决方法,您可以快速实现一个功能完善的jQuery滚动导航菜单。
没有搜到相关的沙龙