<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>下拉菜单</title>
</head>
<style>
*{
margin: 0;
padding: 0;
box-sizing: border-box;
}
html,body{
width: 100%;
height: 62.5%;
}
html{
font-size: 10px;
}
.nav{
width: 100%;
height: 50px;
background-color: bisque;
line-height: 50px;
display: flex;
justify-content: center;
}
.content{
width: 120rem;
list-style: none;
display: flex;
}
.caidanxiang{
font-size: 1.8rem;
height:100%;
text-align: center;
line-height: 5rem;
padding: 0 5rem;
cursor: pointer;
position: relative;
}
.content .item:hover {
background-color: aliceblue;
color: #333;
}
.children{
display: none;
background-color: antiquewhite;
position: absolute;
left: 0;
top: 5rem;
display: none;
}
.children li{
padding: 1rem 2rem;
}
.children li:hover{
background-color: aquamarine;
color: #333;
}
</style>
<body>
<div class="nav">
<ul class="content">
<li class="caidanxiang">首先</li>
<li class="caidanxiang">文章
<ul class="children">
<li>我的文章</li>
<li>发表文章</li>
</ul>
</li>
<li class="caidanxiang">相册
<ul class="children">
<li>我的相册</li>
<li>上传相册</li>
</ul>
</li>
<li class="caidanxiang">消息
<ul class="children">
<li>我的私信</li>
<li>我的留言</li>
</ul>
</li>
</ul>
</div>
<script>
function toggleMenu(parent,is_show){
let child=parent.getElementsByClassNam("children")
if(child.length > 0){
child[0].style.display = is_show
}
}
let _lis = document.getElementsByClassName("item")
for (let i = 0; i < _lis.length; i++) {
_lis[i].onmouseenter = function () {
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>