ハンバーガーメニュー:シンプル(カスケード)|Hamburger Menu: Simple Cascade
HTML
<div class="container">
<div class="hamburger-menu">
<div class="line"></div>
<div class="line"></div>
<div class="line"></div>
</div>
<nav class="nav-menu">
<a href="#">メニュー1</a>
<a href="#">メニュー2</a>
<a href="#">メニュー3</a>
</nav>
</div>
CSS
.container {
position: relative;
width: 100%;
height: 300px;
border: 2px solid #333;
border-radius: 8px;
overflow: hidden;
}
.hamburger-menu {
width: 30px;
height: 22px;
position: absolute;
top: 20px;
right: 20px;
cursor: pointer;
user-select: none;
}
.line {
background-color: black;
height: 4px;
width: 100%;
position: absolute;
transition: all 0.3s;
}
.line:nth-of-type(1) {
top: 0;
}
.line:nth-of-type(2) {
top: 50%;
transform: translateY(-50%);
}
.line:nth-of-type(3) {
bottom: 0;
}
.hamburger-menu.active > .line:nth-of-type(1) {
top: 50%;
transform: translateY(-50%) rotate(45deg);
}
.hamburger-menu.active > .line:nth-of-type(2) {
display: none;
}
.hamburger-menu.active > .line:nth-of-type(3) {
top: 50%;
transform: translateY(-50%) rotate(-45deg);
}
.nav-menu {
display: none;
flex-direction: column;
position: absolute;
top: 0;
left: 50%;
transform: translateX(-50%);
align-items: center;
justify-content: flex-start;
padding: 20px 8px 8px 8px;
width: 33%;
height: 100%;
background-color: #f5f5f5;
opacity: 0;
}
.nav-menu.active {
display: flex;
animation: slideInFromTop 0.3s forwards;
}
@keyframes slideInFromTop {
0% {
top: -100%;
opacity: 0;
}
100% {
top: 0;
opacity: 1;
}
}
.nav-menu a {
text-decoration: none;
color: black;
padding: 5px;
margin: 5px;
opacity: 0;
transform: translateY(-20px);
transition: all 0.3s ease;
pointer-events: none;
}
.nav-menu a.show {
opacity: 1;
transform: translateY(0);
pointer-events: auto;
}
JavaScript
document.querySelector('.hamburger-menu').addEventListener('click', function() {
this.classList.toggle('active');
const navMenu = this.nextElementSibling;
navMenu.classList.toggle('active');
if (navMenu.classList.contains('active')) {
// メニューを順番に表示
const menuItems = navMenu.querySelectorAll('a');
menuItems.forEach((item, index) => {
setTimeout(() => {
item.classList.add('show');
}, 250 + (index * 150)); // メニューウィンドウのアニメーション後から開始
});
} else {
// メニューを非表示
const menuItems = navMenu.querySelectorAll('a');
menuItems.forEach(item => {
item.classList.remove('show');
});
}
});