jQuery 是一个快速、小巧且功能丰富的 JavaScript 库,它简化了 HTML 文档遍历、事件处理、动画和 Ajax 交互。侧边导航(Side Navigation)是一种常见的网页布局方式,通常位于页面的左侧或右侧,用于提供导航链接,帮助用户在不同页面或功能模块之间快速切换。
以下是一个简单的 jQuery 侧边导航示例,展示了如何实现一个可折叠的侧边导航:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>jQuery Side Navigation</title>
<style>
body {
margin: 0;
font-family: Arial, sans-serif;
}
.sidebar {
height: 100%;
width: 0;
position: fixed;
z-index: 1;
top: 0;
left: 0;
background-color: #111;
overflow-x: hidden;
transition: 0.5s;
padding-top: 60px;
}
.sidebar a {
padding: 8px 8px 8px 32px;
text-decoration: none;
font-size: 25px;
color: #818181;
display: block;
transition: 0.3s;
}
.sidebar a:hover {
color: #f1f1f1;
}
.sidebar .closebtn {
position: absolute;
top: 0;
right: 25px;
font-size: 36px;
margin-left: 50px;
}
.openbtn {
font-size: 20px;
cursor: pointer;
background-color: #111;
color: white;
padding: 10px 15px;
border: none;
}
.openbtn:hover {
background-color: #444;
}
#main {
transition: margin-left .5s;
padding: 16px;
}
</style>
</head>
<body>
<div id="mySidebar" class="sidebar">
<a href="javascript:void(0)" class="closebtn" onclick="closeNav()">×</a>
<a href="#">Home</a>
<a href="#">About</a>
<a href="#">Services</a>
<a href="#">Contact</a>
</div>
<div id="main">
<button class="openbtn" onclick="openNav()">☰ Open SideNav</button>
<h2>Main Content</h2>
<p>This is the main content area.</p>
</div>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
function openNav() {
$("#mySidebar").css("width", "250px");
$("#main").css("margin-left", "250px");
}
function closeNav() {
$("#mySidebar").css("width", "0");
$("#main").css("margin-left", "0");
}
</script>
</body>
</html>
通过以上示例代码和常见问题解决方法,你应该能够实现一个基本的 jQuery 侧边导航,并解决一些常见的问题。
领取专属 10元无门槛券
手把手带您无忧上云