基于jQuery实现侧边栏隐藏和显示导航

先上代码(实现思路在最后):

index.html + jQuery:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>折叠式菜单特效</title>
		<link rel="stylesheet" href="css/menu.css">
		<script src="js/jquery-1.12.4.js"></script>

		<script>
			$("document").ready(function() {

				$(".menu_head+div").hide();
				$(".menu_head").click(function() {
					//判断我们下面所有标签中是否含有标记的标签list
					if ($(this).nextAll().hasClass("list")) {
						//如果有的话就显示全部
						$(this).nextAll().show();
						//同时把list标签去掉.再次进这个条件语句的时候就执行else
						$(this).nextAll().removeClass("list");
						$(this).css("background","#f1f1f1 url(img/pro_down.png) center right no-repeat");
					} else {
						//如果没有list的话就把所有的标签都隐藏起来
						$(this).nextAll().hide();
						//隐藏好了之后就加上list
						$(this).nextAll().addClass("list");
						$(this).css("background","#f1f1f1 url(img/pro_left.png) center right no-repeat");
					}
				});
			});
		</script>
	</head>
	<body>
		<ul class="menu_list">
			<li>
				<p class="menu_head">目标管理</p>
				<div class="menu_body">
					<a href="#">主题空间</a>
					<a href="#">项目任务</a>
					<a href="#">工作计划</a>
					<a href="#">日程事件</a>
					<a href="#">时间视图</a>
				</div>
				<span class="list"></span>
			</li>
			
			<li>
				<p class="menu_head">会议管理</p>
				<div class="menu_body">
					<a href="#">主题空间</a>
					<a href="#">会议安排</a>
					<a href="#">待开会议</a>
					<a href="#">已开会议</a>
					<a href="#">会议资源</a>
				</div>
				<span class="list"></span>
			</li>
			
			<li>
				<p class="menu_head">知识社区</p>
				<div class="menu_body">
					<a href="#">我的收藏</a>
					<a href="#">知识广场</a>
					<a href="#">文档中心</a>
					<a href="#">我的博客</a>
					<a href="#">文档库管理</a>
				</div>
				<span class="list"></span>
			</li>
			
			<li>
				<p class="menu_head">我的工具</p>
				<div class="menu_body">
					<a href="#">综合查询</a>
					<a href="#">通讯录</a>
					<a href="#">便签</a>
					<a href="#">计算器</a>
					<a href="#">万年历</a>
					<a href="#">常用链接</a>
				</div>
				<span class="list"></span>
			</li>
		</ul>
	</body>
</html>

css代码 :

* {
    padding: 0;
    margin: 0;
}
ul {
    list-style-type: none;
    margin: 100px;
}
.menu_head {
    width: 185px;
    height: 47px;
    line-height: 47px;
    padding-left: 38px;
    font-size: 17px;
    color: #475052;
    cursor: pointer;
    border: 1px solid #e1e1e1;
    position: relative;
    margin: 0px;
    font-weight: bold;
    background: #f1f1f1 url(../img/pro_left.png) center right no-repeat;
}
.menu_list .current {
    background: #f1f1f1 url(../img/pro_down.png) center right no-repeat;
}
.menu_body {
    width: 223px;
    height: auto;
    overflow: hidden;
    line-height: 38px;
    border-left: 1px solid #e1e1e1;
    backguound: #fff;
    border-right: 1px solid #e1e1e1;
}
.menu_body a {
    display: block;
    width: 223px;
    height: 38px;
    line-height: 38px;
    padding-left: 38px;
    color: #777;
    background: #fff;
    text-decoration: none;
    border-bottom: 1px solid #e1e1e1;
}

实现效果(分别是点击前和点击后):

实现思路:

       一开始的时候我默认在<div class="menu_body">中加入了一个没有任何内容的<span class="list">的一个标记标签,这个标签用于判断我们当前是点击展开还是点击收回用的。

        默认情况下,当我们点击menu_head的导航栏之后,我们通过jQuery中.click的方式来触发函数(前提是我们先把所有的menu_body和div内容隐藏了起来,设定为hide),通过判断当前this对象点击的nextAll()是否包含一个类名为list的元素,假如说有的话就把所有的list删掉,同时把当前this对象下的所有内容show()出来,最后把图片一下即可。

        倘若我们想再次点击收回菜单内容,我们发现所有的list已经被删除了,所以我们得通过addClass的方式把类名为list的<span>重新加上来,这样的话我们下一次再次点击展开的时候相当于又恢复成了默认的情况。最后再把当前对象下所有的内容hide()起来就可以了。

 

 

 

  • 1
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个使用Bootstrap和jQuery实现导航侧边栏的HTML代码示例: ```html <!DOCTYPE html> <html> <head> <title>Navigation Sidebar Example</title> <!-- Bootstrap CSS --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"> <!-- jQuery --> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <!-- Bootstrap JavaScript --> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script> <style> /* Sidebar styling */ .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; } /* Page content styling */ #main { transition: margin-left .5s; padding: 20px; } /* Responsive media query for smaller screens */ @media screen and (max-height: 450px) { .sidebar {padding-top: 15px;} .sidebar a {font-size: 18px;} } </style> </head> <body> <!-- Navigation sidebar --> <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> <!-- Page content --> <div id="main"> <button class="openbtn" onclick="openNav()">☰ Navigation</button> <h1>Welcome to my website!</h1> <p>This is an example of how to create a navigation sidebar using Bootstrap and jQuery.</p> </div> <script> // Open the sidebar function openNav() { document.getElementById("mySidebar").style.width = "250px"; document.getElementById("main").style.marginLeft = "250px"; } // Close the sidebar function closeNav() { document.getElementById("mySidebar").style.width = "0"; document.getElementById("main").style.marginLeft = "0"; } </script> </body> </html> ``` 这段代码实现了一个具有导航侧边栏的网页,点击“Navigation”按钮可以打开或关闭侧边栏。该示例使用了Bootstrap的CSS和JavaScript库以及jQuery库来实现导航侧边栏的样式和交互。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值