Correcting teacher:WJ
Correction status:qualified
Teacher's comments:总体写的不错,以后要贴上效果图就更好点了
事件触发阶段类型 | 特征 | 参数设置 |
---|---|---|
冒泡 | 由内向外传递 | addEventListener(事件类型,事件方法,false),最后一个参数可省略,默认就是冒泡方法 |
捕获 | 由外向内传递 | addEventListener(事件类型,事件方法,true) |
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>滑动样式</title>
<style>
*{
margin: 0;
padding: 0;
}
a{
text-decoration: none;
}
body{
display:static;
}
.header{
width: 100%;
background: #000;;
}
.content{
margin: 0 auto;
width: 980px;
}
ul,dl{
list-style: none;
}
ul.nav::after{
content: '';
display: block;
width: 100%;
clear: both;
}
.nav>li{
padding: 0px 20px;
float: left;
cursor: pointer;
position: relative;
}
.nav>li a{
line-height: 80px;
color: #fff;
}
.nav>li:hover{
background-color: #363c41;
}
.nav-active{
background-color: #363c41;
}
.nav-li-chlid{
display: none;
position:absolute;
top: 80px;
left: 0px;
width: 200px;
color: #363c41;
background-color: #fff;
box-shadow:0 2px 4px rgba(0,0,0,.12);
border: rgba(0,0,0,.12) 1px solid;
animation-name: navLiAnmation;
animation-duration: 0.3;
animation-fill-mode: both;
}
.nav-li-chlid>dd{
float: left;
padding: 10px;
text-align: center;
box-sizing: border-box;
display: inline-block;
width: 50%;
}
.nav-li-chlid>dd a{
line-height: 30px;
color: #363c41;
}
@keyframes navLiAnimation {
0%{
transform: translate3d(0,30px,0);
opacity: .3;
}
100%{
transform: translate3d(0,0,0);
opacity: 1;
}
}
</style>
</head>
<body>
<div class="header">
<ul class="nav content">
<li>
<a href="">首页</a>
</li>
<li>
<a href="">入门教程</a>
</li>
<li>
<a href="">社区问答</a>
<dl class="nav-li-chlid">
<dd>
<a>社区问答</a>
</dd>
<dd>
<a>社区问答</a>
</dd>
<dd>
<a>社区问答</a>
</dd>
<dd>
<a>社区问答</a>
</dd>
<dd>
<a>社区问答</a>
</dd>
<dd>
<a>头条</a>
</dd>
</dl>
</li>
<li>
<a href="">技术文章</a>
<dl class="nav-li-chlid">
<dd>
<a>技术文章</a>
</dd>
<dd>
<a>技术文章</a>
</dd>
<dd>
<a>技术文章</a>
</dd>
<dd>
<a>技术文章</a>
</dd>
<dd>
<a>头条</a>
</dd>
<dd>
<a>头条</a>
</dd>
</dl>
</li>
<li>
<a href="">资料下载</a>
<dl class="nav-li-chlid">
<dd>
<a>资料下载</a>
</dd>
<dd>
<a>资料下载</a>
</dd>
<dd>
<a>资料下载</a>
</dd>
<dd>
<a>资料下载</a>
</dd>
<dd>
<a>资料下载</a>
</dd>
<dd>
<a>资料下载</a>
</dd>
</dl>
</li>
<li>
<a href="">php培训</a>
<dl class="nav-li-chlid">
<dd>
<a>php培训</a>
</dd>
<dd>
<a>php培训</a>
</dd>
<dd>
<a>php培训</a>
</dd>
<dd>
<a>php培训</a>
</dd>
<dd>
<a>php培训</a>
</dd>
<dd>
<a>php培训</a>
</dd>
</dl>
</li>
</ul>
</div>
</body>
<script>
const navs = document.querySelectorAll('.nav > li');
navs.forEach(function(nav){
nav.addEventListener('mouseover',showSubMenu);
nav.addEventListener('mouseout',closeSubMenu)
});
// 显示子菜单
function showSubMenu(event){
console.log(event.target)
//target是指触发事件的元素,currentTarget是指绑定该触发事件的元素
//搞清楚node节点与节点元素,nextElementSibling获取到的是相邻的兄弟节点元素,nextSibling获取到的是相邻的兄弟节点
//其中通过firstChild和lastChild获得HTML Node是不可取的。因为,根据浏览器的不同,firstChild有可能返回parentObj的属性对象。
if(event.target.nextElementSibling!==null){
event.target.nextElementSibling.style.display = 'block'
}
}
// 关掉子菜单
function closeSubMenu(event){
if(event.target.nodeName==='A' && event.target.nextElementSibling!==null){
event.target.nextElementSibling.style.display = 'none'
}
}
</script>
</html>