ハンバーガーメニュー:シンプル(オーバーレイ)|Hamburger Menu: Simple Overlay
HTML
<div class="container">
<div class="hamburger-menu">
<div class="line"></div>
<div class="line"></div>
<div class="line"></div>
</div>
<div class="overlay"></div>
<nav class="nav-menu">
<a href="#">Menu 1</a>
<a href="#">Menu 2</a>
<a href="#">Menu 3</a>
<a href="#">Menu 4</a>
<a href="#">Menu 5</a>
<a href="#">Menu 6</a>
<a href="#">Menu 7</a>
<a href="#">Menu 8</a>
<a href="#">Menu 9</a>
<a href="#">Menu 10</a>
</nav>
</div>
CSS
/* コンテナ */
.container {
position: relative;
width: 100%;
height: 300px;
background: #f7f7f7;
}
/* ハンバーガーメニュー */
.hamburger-menu {
width: 30px;
height: 22px;
position: absolute;
top: 20px;
right: 20px;
cursor: pointer;
user-select: none;
z-index: 1001;
}
.hamburger-menu .line {
background-color: black;
height: 4px;
width: 100%;
position: absolute;
transition: all 0.3s;
}
.hamburger-menu .line:nth-of-type(1) { top: 0; }
.hamburger-menu .line:nth-of-type(2) { top: 50%; transform: translateY(-50%); }
.hamburger-menu .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);
}
/* オーバーレイ */
.overlay {
position: absolute;
top: 0; left: 0; width: 100%; height: 100%;
background-color: rgba(0, 0, 0, 0.1);
opacity: 0; visibility: hidden;
transition: all 0.3s ease; z-index: 999;
}
.overlay.active { opacity: 1; visibility: visible; }
/* メニュー */
@keyframes fadeIn {
0% { top: 45%; opacity: 0; }
100% { top: 50%; opacity: 1; }
}
.nav-menu {
display: none; flex-direction: column;
position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%);
align-items: center; justify-content: flex-start; padding: 20px;
width: 300px; background-color: #fff; border-radius: 10px;
box-shadow: 0 10px 30px rgba(0,0,0,0.3); opacity: 0; z-index: 1000;
max-height: 80%; overflow-y: auto;
}
.nav-menu.active { display: flex; animation: fadeIn 0.5s forwards; }
.nav-menu a {
text-decoration: none; color: black; padding: 10px 20px; margin: 5px;
border-radius: 5px; transition: all 0.3s; opacity: 0;
transform: translateY(-20px); pointer-events: none;
}
.nav-menu a.show { opacity: 1; transform: translateY(0); pointer-events: auto; }
.nav-menu a:hover { background-color: #f0f0f0; }
JavaScript
document.querySelectorAll('.hamburger-menu').forEach(hamburgerMenu => {
const container = hamburgerMenu.parentElement;
const navMenu = container.querySelector('.nav-menu');
const overlay = container.querySelector('.overlay');
hamburgerMenu.addEventListener('click', () => {
hamburgerMenu.classList.toggle('active');
navMenu.classList.toggle('active');
if (navMenu.classList.contains('active')) {
overlay.classList.add('active');
setTimeout(() => {
const menuItems = navMenu.querySelectorAll('a');
menuItems.forEach((item, index) => {
setTimeout(() => { item.classList.add('show'); }, index * 150);
});
}, 300);
} else {
const menuItems = navMenu.querySelectorAll('a');
menuItems.forEach(item => item.classList.remove('show'));
setTimeout(() => { overlay.classList.remove('active'); }, 200);
}
});
});