使用工具 VsCode
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>下拉菜单</title>
<style>
*{margin: 0;padding: 0;box-sizing: border-box;}
html,body{height: 100%;height: 100%;}
html,
body{
width: 100%;
height: 100%;
}
html{
font-size: 10px;
}
.nav{
width: 100%;
height: 5rem;
background-color: azure;
display: flex;
justify-content: center;
}
.content{
width: 120rem;
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 .content:hover{
background-color: aliceblue;
color: #333;
}
.children{
list-style: none;
background-color: antiquewhite;
position: absolute;
left: 0;
top: 5rem;
display: none;
}
.children li{
padding: 1rem 2rem;
}
.children li:hover{
background-color: beige;
color: #333;
}
</style>
</head>
<body>
<div class="nav">
<div 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>
</div>
</div>
<script>
function toggleMenu(parent,is_show){
let child=parent.getElementsByClassName("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(){
toggleMenu(_lis[i],"block")
}
_lis[i].onmouseleave=function(){
toggleMenu(_lis[i],"none")
}
}
</script>
</body>
</html>