作业分析:下拉菜单效果
html标签
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="./css/demo01.css">
</head>
<body>
<!-- 背景 -->
<div class="nav">
<!-- 布局 -->
<ul class="content">
<li class="item">首页</li>
<li class="item">文章
<ul class="children">
<li>我的文章</li>
<li>发表文章</li>
</ul>
</li>
<li class="item">相册
<ul class="children">
<li>我的相册</li>
<li>上传照片</li>
</ul>
</li>
<li class="item">消息
<ul class="children">
<li>我的私信</li>
<li>我的留言</li>
</ul>
</li>
</ul>
</div>
<script>
let _lis = document.getElementsByClassName("item")
for (let i = 0; i < _lis.length; i++) {
_lis[i].onmouseenter = function () {
// 让子标签ul.children显示/隐藏
let child = _lis[i].getElementsByClassName("children")
if (child.length > 0) {
child[0].style.display = "block"
}
}
// 添加鼠标离开事件
_lis[i].onmouseleave = function () {
let child = _lis[i].getElementsByClassName("children")
if (child.length > 0) {
child[0].style.display = "none"
}
}
}
</script>
</body>
</html>
css
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
}
html {
font-size: 10px;
}
/* 页面样式 */
.nav {
width: 100%;
height: 5rem;
/* 1rem = 10px*/
background-color: rgb(98, 212, 225);
display: flex;
justify-content: center;
}
.content {
width: 120rem;
/* background-color:aquamarine; */
list-style: none;
display: flex;
}
.content .item {
font-size: 1.8rem;
height: 100%;
padding: 0 5rem;
text-align: center;
line-height: 5rem;
cursor: pointer;
position: relative;
}
.content .item:hover {
background-color: aliceblue;
color: #333;
}
.children {
list-style: none;
background-color: rgb(91, 222, 100);
position: absolute;
left: 0;
top: 5rem;
display: none;
}
.children li {
padding: 1rem 2rem;
}
.children li:hover {
background-color: rgb(230, 154, 66);
color: #333;
}