jQuery 是一个快速、小巧且功能丰富的 JavaScript 库,它简化了 HTML 文档遍历、事件处理、动画和 Ajax 交互。楼层数导航效果通常是指在一个页面上有多个部分(楼层),用户可以通过点击导航链接快速跳转到页面的特定部分。
href
属性为锚点(如 #section1
),点击链接后页面会滚动到对应的元素位置。以下是一个简单的 jQuery 楼层导航效果的示例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>jQuery 楼层导航效果</title>
<style>
body {
font-family: Arial, sans-serif;
}
.nav {
position: fixed;
top: 20px;
right: 20px;
}
.nav a {
display: block;
margin-bottom: 10px;
text-decoration: none;
color: #333;
}
.section {
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
border-bottom: 1px solid #ccc;
}
</style>
</head>
<body>
<div class="nav">
<a href="#section1">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 src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function() {
$('.nav a').click(function(event) {
event.preventDefault();
var target = $(this).attr('href');
$('html, body').animate({
scrollTop: $(target).offset().top
}, 1000);
});
});
</script>
</body>
</html>
href
属性正确指向目标元素的 id
。id
属性值正确且唯一。scrollTop
的值正确计算。1000
毫秒)以达到预期效果。$(document).ready()
确保 DOM 完全加载后再执行脚本。通过以上方法,可以实现一个简单且效果良好的 jQuery 楼层导航效果。
领取专属 10元无门槛券
手把手带您无忧上云