点击一级菜单则展开二级菜单
展开收起动画效果
完整代码,需要引入jQuery.JS文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
* {
padding: 0;
margin: 0;
}
.nav {
list-style: none;
width: 300px;
margin: 100px auto;
}
.nav > li {
border: 1px solid #000;
line-height: 35px;
border-bottom: none;
position: relative;
}
.nav > li:last-child {
border-bottom: 1px solid #000;
border-bottom-left-radius: 10px;
border-bottom-right-radius: 10px;
}
.nav > li:first-child {
border-top-left-radius: 10px;
border-top-right-radius: 10px;
}
li {
list-style: none;
text-align: center;
}
.nav > li > span {
display: inline-block;
width: 32px;
height: 32px;
position: absolute;
right: 10px;
top: 5px;
}
.sub > li {
background: plum;
border: 1px solid white;
}
.sub > li:hover {
background: red;
}
.sub {
display: none;
}
.nav > .current > span {
transform: rotate(90deg);
}
</style>
<title>Title</title>
<script src="WEB-INF/js/jquery-1.12.4.js"></script>
<script>
$(function () {
//1,监听一级菜单点击事件
$(".nav>li").click(function () {
//拿到二级菜单
var $sub = $(this).children(".sub");
//展开
$sub.slideDown(1000);
//拿到所有非当前的二级菜单
var $othersub = $(this).siblings().children(".sub");
$othersub.slideUp(1000);
//让所有非当前二级菜单缩
//
$(this).addClass("current");
//还原
$(this).siblings().removeClass("current")
})
});
</script>
</head>
<body>
<ul class="nav">
<li>一级菜单<span> > </span>
<ul class="sub">
<li>二级菜单</li>
<li>二级菜单</li>
<li>二级菜单</li>
<li>二级菜单</li>
<li>二级菜单</li>
</ul>
</li>
<li>一级菜单<span> > </span>
<ul class="sub">
<li>二级菜单</li>
<li>二级菜单</li>
<li>二级菜单</li>
<li>二级菜单</li>
<li>二级菜单</li>
</ul>
</li>
<li>一级菜单<span> > </span>
<ul class="sub">
<li>二级菜单</li>
<li>二级菜单</li>
<li>二级菜单</li>
<li>二级菜单</li>
<li>二级菜单</li>
</ul>
</li>
<li>一级菜单<span> > </span>
<ul class="sub">
<li>二级菜单</li>
<li>二级菜单</li>
<li>二级菜单</li>
<li>二级菜单</li>
<li>二级菜单</li>
</ul>
</li>
<li>一级菜单<span> > </span>
<ul class="sub">
<li>二级菜单</li>
<li>二级菜单</li>
<li>二级菜单</li>
<li>二级菜单</li>
<li>二级菜单</li>
</ul>
</li>
<li>一级菜单<span> > </span>
<ul class="sub">
<li>二级菜单</li>
<li>二级菜单</li>
<li>二级菜单</li>
<li>二级菜单</li>
<li>二级菜单</li>
</ul>
</li>
<li>一级菜单<span> > </span>
<ul class="sub">
<li>二级菜单</li>
<li>二级菜单</li>
<li>二级菜单</li>
<li>二级菜单</li>
<li>二级菜单</li>
</ul>
</li>
<li>一级菜单<span> > </span>
<ul class="sub">
<li>二级菜单</li>
<li>二级菜单</li>
<li>二级菜单</li>
<li>二级菜单</li>
<li>二级菜单</li>
</ul>
</li>
</ul>
</body>
</html>