ハンバーガーメニューを自作するのが面倒な人へ捧ぐ。
HTML
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>hamburger practice</title>
<link rel="stylesheet" href="./style.css" />
</head>
<body>
<nav class="nav">
<button class="hamburger">
<span></span>
<span></span>
<span></span>
</button>
<div class="menu">
<ul>
<li><a href="#home">Home</a></li>
<li><a href="#about">About us</a></li>
<li><a href="#service">Service</a></li>
<li><a href="#contact">Contact</a></li>
</ul>
</div>
</nav>
<script src="./script.js"></script>
</body>
</html>
CSS
/* リセットCSS */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.nav {
position: fixed;
top: 0;
left: 0;
width: 100%;
padding: 1rem;
background-color: #fff;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
}
.hamburger {
position: relative;
width: 30px;
height: 24px;
cursor: pointer;
z-index: 100;
border: none;
background: none;
}
.hamburger span {
position: absolute;
left: 0;
width: 100%;
height: 2px;
background-color: #333;
transition: all 0.4s;
}
.hamburger span:nth-child(1) {
top: 0;
}
.hamburger span:nth-child(2) {
top: 11px;
}
.hamburger span:nth-child(3) {
bottom: 0;
}
.hamburger.active span:nth-child(1) {
transform: translateY(11px) rotate(-45deg);
background-color: #fff;
}
.hamburger.active span:nth-child(2) {
opacity: 0;
background-color: #fff;
}
.hamburger.active span:nth-child(3) {
transform: translateY(-11px) rotate(45deg);
background-color: #fff;
}
.menu {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100vh;
background-color: rgba(0,0,0,0.9);
color: #fff;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
transform: translateX(100%);
transition: transform 0.4s;
}
.menu.active {
transform: translateX(0);
}
.menu ul {
list-style: none;
}
.menu li {
margin: 1.5rem 0;
font-size: 1.5rem;
opacity: 0;
transform: translateX(20px);
transition: all 0.4s;
}
.menu.active li {
opacity: 1;
transform: translateX(0);
}
/* メニュー項目の遅延アニメーション */
.menu.active li:nth-child(1) { transition-delay: 0.2s; }
.menu.active li:nth-child(2) { transition-delay: 0.3s; }
.menu.active li:nth-child(3) { transition-delay: 0.4s; }
.menu.active li:nth-child(4) { transition-delay: 0.5s; }
/* メニューリンク */
.menu a {
color: #fff;
text-decoration: none;
position: relative;
}
.menu a::after {
content: '';
position: absolute;
bottom: -4px;
left: 0;
width: 100%;
height: 2px;
background-color: #fff;
transform: scaleX(0);
transition: transform 0.3s;
transform-origin: right;
}
.menu a:hover::after {
transform: scaleX(1);
transform-origin: left;
}
JavaScript
const hamburger = document.querySelector(".hamburger");
const menu = document.querySelector(".menu");
hamburger.addEventListener("click", () => {
hamburger.classList.toggle("active");
menu.classList.toggle("active");
});
document.querySelectorAll(".menu a").forEach((link) => {
link.addEventListener("click", () => {
hamburger.classList.remove("active");
menu.classList.remove("active");
});
});
document.addEventListener("click", (e) => {
if (!hamburger.contains(e.target) && !menu.contains(e.target)) {
hamburger.classList.remove("active");
menu.classList.remove("active");
}
});