jQuery侧边划出导航菜单是一种常见的网页交互设计,通常用于移动设备或响应式网页设计中。这种菜单通常隐藏在屏幕边缘,用户可以通过滑动或点击按钮来展开或收起。
以下是一个简单的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 Menu</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;
}
.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="#">Home</a>
<a href="#">About</a>
<a href="#">Services</a>
<a href="#">Contact</a>
</div>
<div id="main">
<button class="openbtn" onclick="openNav()">☰ Open Menu</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");
}
$(document).on('click', function(event) {
if (!$(event.target).closest('#mySidebar, .openbtn').length) {
closeNav();
}
});
</script>
</body>
</html>
通过以上示例代码和常见问题解决方法,你应该能够实现一个基本的jQuery侧边划出导航菜单,并解决一些常见问题。
没有搜到相关的沙龙
领取专属 10元无门槛券
手把手带您无忧上云