一、今天来总结一下如何利用列表标签创建一个纵向弹跳的二级菜单,其实是很简单的一些知识点,二级菜单在写HTML这一块中运用最多的大概就是列表标签ul、li,其次就是超链接a,然后js里面也是运用了简单的当鼠标点击和鼠标离开的时候添加了事件监听,具体代码如下:
HTML:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<link rel="stylesheet" href="css/public.css">
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<!-- 二级菜单 -->
<nav class="pagenav">
<div class="background">
<div class="box">
<img src="img/box.png" >
<ul class="pagenav_ul" id="navUl">
<li>
<a href="http://www.baidu.com">首页</a>
<ul class="submenu show">
<li><a href="http://www.chorome.com">腾讯客服</a></li>
<li><a href="http://www.chorome.com">周边商城</a></li>
</ul>
</li>
<li>
<a href="http://www.baidu.com">游戏资料</a>
<ul class="submenu">
<li><a href="http://www.chorome.com">游戏特色</a></li>
<li><a href="http://www.chorome.com">职业介绍</a></li>
<li><a href="http://www.chorome.com">新手指南</a></li>
<li><a href="http://www.chorome.com">攻略中心</a></li>
</ul>
</li>
<li>
<a href="http://www.baidu.com">下载中心</a>
<ul class="submenu">
<li><a href="http://www.chorome.com">下载客户端</a></li>
<li><a href="http://www.chorome.com">南天国体验服</a></li>
<li><a href="http://www.chorome.com">特色新区</a></li>
<li><a href="http://www.chorome.com">开箱有礼</a></li>
</ul>
</li>
<li>
<a href="http://www.baidu.com">自助服务</a>
<ul class="submenu ">
<li><a href="http://www.chorome.com">安全模式</a></li>
<li><a href="http://www.chorome.com">处罚减免</a></li>
<li><a href="http://www.chorome.com">封停直播</a></li>
<li><a href="http://www.chorome.com">工作室清除</a></li>
</ul>
</li>
<li>
<a href="http://www.baidu.com">玩家互动</a>
<ul class="submenu">
<li><a href="http://www.chorome.com">灵芝圈社区</a></li>
<li><a href="http://www.chorome.com">剑灵微博</a></li>
</ul>
</li>
<li>
<a href="http://www.baidu.com">登录</a>
<ul class="submenu">
<li><a href="http://www.chorome.com">个人中心</a></li>
</ul>
</li>
</ul>
</div>
</div>
</nav>
<!-- 二级菜单 end-->
<script src="js/app.js"></script>
</body>
</html>
style.css:
.pagenav{
width: 800px;
height: 1000px;
background: #00ffff;
margin-left: auto;
margin-right: auto;
}
.background{
width: 300px;
height: 1000px;
background: #c08cd9;
}
.box{
width: 280px;
height: 1000px;
background: #5f0f85;
}
.box img{
margin-left: 73px;
margin-top: 56px;
margin-bottom: 10px;
}
.pagenav_ul > li > a{
font-size: 20px;
font-family: "arial rounded mt bold";
color: #fff;
padding-top: 30px;
padding-bottom: 30px;
transition:all 0.5s;
text-align: center;
display: block;
}
.pagenav_ul >li{
position: relative;
background: #5f0f85;
}
.pagenav_ul{
line-height: 50px;
}
.pagenav_ul > li > a:hover{
background: #3b0a52;
}
.pagenav_ul > li:hover > a{
background: #3b0a52;
}
.submenu{
width: 200px;
text-align: center;
position: absolute;
top: 0px;
left: 280px;
display: none;
}
.submenu>li>a{
color: #fff;
display: block;
font-size: 20px;
line-height: 50px;
}
.submenu>li>a:hover{
background: #f80;
}
.submenu>li{
background: #3b0a52;
}
.submenu.show{
display: block;
}
.submenu.show>li:nth-child(1){
animation: showMenu 0.5s both;
}
.submenu.show>li:nth-child(2){
animation: showMenu 0.5s 0.2s both;
}
.submenu.show>li:nth-child(3){
animation: showMenu 0.5s 0.4s both;
}
.submenu.show>li:nth-child(4){
animation: showMenu 0.5s 0.6s both;
}
@keyframes showMenu{
0%{
opacity: 0;
transform: scale(0,0);
}
100%{
opacity: 1;
transform: scale(1,1);
}
}
public.css
*{
padding: 0;
margin: 0;
}
a{
text-decoration: none;
}
li,ol,ul{
list-style: none;
}
.clear{
clear: both;
height: 0;
line-height: 0;
overflow: hidden;
font-size: 0;
}
.body{
font-family: "microsoft yahei",verdana,"pingFang SC",simsun;
}
textarea{
resize: none;
}
button,
input[type=submit],
input[type=button],
input[type=reset]
{
cursor: pointer;
}
js:
let navUl=document.getElementById("navUl");
let li=navUl.children;
function doEnter()
{
//console.info("enter");
let ul=this.getElementsByClassName("submenu")[0];
if(!ul)
{
return null;
}
//ul.style.display="block";
ul.classList.add("show")
}
function doLeave()
{
//console.info("leave");
let ul=this.getElementsByClassName("submenu")[0];
if(!ul)
{
return null;
}
//ul.style.display="none";
ul.classList.remove("show")
}
for(let i=0;i<=li.length-1;i++)
{
li[i].addEventListener("mouseenter",doEnter);
li[i].addEventListener("mouseleave",doLeave);
}
看一下最后的结果吧