<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
html, body {
width: 100%;
display: flex;
flex-direction: column;
align-items: center;
background-color: #f4f4f4;
}
body {
font-size: 62.5%;
}
.nav {
width: 1200px;
height: 80px;
background-color: bisque;
display: flex;
justify-content: center;
border-radius: 10px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
position: fixed;
top: 0;
}
.page-header {
width: 100%;
height: 100%;
display: flex;
list-style: none;
justify-content: center;
}
.page-header .item {
padding: 10px 50px;
font-size: 18px;
text-align: center;
cursor: pointer;
position: relative;
transition: background-color 0.3s ease;
}
.page-header .item:hover {
background-color: #f2f2f1;
color: #333;
font-weight: 500;
}
.children {
display: none;
position: absolute;
top: 80px;
left: 0;
list-style: none;
padding: 10px 20px;
background-color: aquamarine;
line-height: 20px;
color: #f2f2f1;
border-radius: 5px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}
.children li {
line-height: 50px;
transition: background-color 0.3s ease;
}
.children li:hover {
background-color: #f2f2f1;
color: #333;
}
</style>
</head>
<body>
<nav class="nav">
<ul class="page-header">
<li class="item">首页</li>
<li class="item">文章
<ul class="children">
<li>发表文章</li>
<li>我的文章</li>
<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>
</nav>
<script>
let _lis = document.getElementsByClassName("item")
console.log(_lis)
for(let i = 0; i < _lis.length; i++){
_lis[i].onmouseenter = function() {
let _children = _lis[i].getElementsByClassName("children")
if(_children.length > 0) {
let c = _children[0]
c.style.display = "block"
}
}
_lis[i].onmouseleave = function() {
let _children = _lis[i].getElementsByClassName("children")
if(_children.length > 0) {
let c = _children[0]
c.style.display = "none"
}
}
}
</script>
</body>
</html>
2025-03-31
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- 九、CSS动画 CSS动画给网页增添了生命力,从微妙的过渡效果到复杂的关键帧动画,CSS提供了强大的工具来创建流畅...