HTML
<div class="hamburger-wrapper">
<button class="box-button" id="hamburger-btn">
<div class="bar top"></div>
<div class="bar bottom"></div>
</button>
<div class="menu-overlay" id="menu-overlay">
<ul>
<li><a href="#">TOP</a></li>
<li><a href="#">ABOUT</a></li>
<li><a href="#">WORKS</a></li>
<li><a href="#">CONTACT</a></li>
</ul>
</div>
</div>
CSS
.hamburger-wrapper {
display: flex;
justify-content: center;
align-items: center;
padding: 40px 0;
position: relative;
z-index: 10;
}
/* ボックス型ボタン */
.box-button {
width: 60px;
height: 50px;
background: none;
border: none;
position: relative;
cursor: pointer;
z-index: 20;
}
.box-button .bar {
position: absolute;
width: 100%;
height: 6px;
background-color: #333;
transition: all 0.4s ease;
border-radius: 3px;
}
.box-button .top {
top: 10px;
}
.box-button .bottom {
bottom: 10px;
}
.box-button.open .top {
transform: translateY(-15px) rotate(45deg);
}
.box-button.open .bottom {
transform: translateY(15px) rotate(-45deg);
}
/* メニュー本体 */
.menu-overlay {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(17, 17, 17, 0.95);
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
transform: scale(0);
opacity: 0;
transition: transform 0.5s ease, opacity 0.5s ease;
z-index: 5;
pointer-events: none;
}
.menu-overlay.open {
transform: scale(1);
opacity: 1;
pointer-events: auto;
}
.menu-overlay ul {
list-style: none;
padding: 0;
margin: 0;
text-align: center;
}
.menu-overlay ul li {
margin: 20px 0;
}
.menu-overlay ul li a {
color: #fff;
text-decoration: none;
font-size: 2rem;
font-weight: bold;
transition: color 0.3s;
}
.menu-overlay ul li a:hover {
color: #d60000;
}
JavaScript
const btn = document.getElementById("hamburger-btn");
const menu = document.getElementById("menu-overlay");
btn.addEventListener("click", function () {
btn.classList.toggle("open");
menu.classList.toggle("open");
});
menu.addEventListener("click", function (e) {
if (e.target === menu) {
btn.classList.remove("open");
menu.classList.remove("open");
}
});