效果
代码
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div id="toggle-icon" onclick="menuToggle()"></div>
<div id="menu-overlay">
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Services</a></li>
<li><a href="#">Portfolio</a></li>
<li><a href="#">Our Team</a></li>
<li><a href="#">Contact</a></li>
</ul>
</div>
<script>
function menuToggle() {
const toggleIcon = document.getElementById("toggle-icon");
toggleIcon.classList.toggle("active");
const nav = document.getElementById("menu-overlay")
nav.classList.toggle("active")
}
</script>
</body>
</html>
style.css
* {
margin: 0;
padding: 0;
}
#menu-overlay {
position: fixed;
top: 0;
right: 0;
bottom: 0;
left: 0;
background-color: red;
display: flex;
justify-content: center;
align-items: center;
overflow-y: auto;
transform: scale(0);
transition: 0.5s;
}
#menu-overlay.active {
transform: scale(1);
}
#menu-overlay ul li {
list-style: none;
text-align: center;
}
#menu-overlay ul li a {
position: relative;
display: inline-block;
text-decoration: none;
font-size: 4em;
color: #fff;
font-weight: 700;
text-transform: uppercase;
}
#menu-overlay ul li a::before {
content: '';
position: absolute;
top: 50%;
left: 0;
width: 100%;
height: 8px;
background: #ff0;
transform: translateY(-50%) scaleX(0);
transform-origin: right;
transition: transform 0.5s;
}
#menu-overlay ul li a:hover::before {
transform: translateY(-50%) scaleX(1);
transform-origin: left;
}
#toggle-icon {
position: fixed;
top: 20px;
right: 20px;
width: 50px;
height: 50px;
background: #ff0 url("./assets/open.png") no-repeat;
z-index: 1;
cursor: pointer;
}
#toggle-icon.active {
background-image: url("./assets/close.png");
}
素材