ハンバーガーメニュー:ミニマル|Hamburger Menu: Minimal

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
@keyframes fadeIn {
    0% {
        top: 45%;
        opacity: 0;
    }
    
    100% {
        top: 50%;
        opacity: 1;
    }
}

.container {
    position: relative;
    width: 100%;
    height: 300px;
}

.hamburger-menu {
    width: 25px;
    height: 18px;
    position: absolute;
    top: 20px;
    right: 20px;
    cursor: pointer;
    user-select: none;
}

.line {
    background-color: #666;
    height: 1px;
    width: 100%;
    position: absolute;
    transition: all 0.2s ease;
}

.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) {
    opacity: 0;
}

.hamburger-menu.active > .line:nth-of-type(3) {
    bottom: 50%;
    transform: translateY(50%) rotate(-45deg);
}

.nav-menu {
    display: none;
    flex-direction: column;
    position: absolute;
    top: 45%;
    left: 50%;
    transform: translate(-50%, -50%);
    align-items: center;
    justify-content: center;
    padding: 15px;
    width: 50%;
    background: rgba(255,255,255,0.95);
    border: 1px solid #eee;
    opacity: 0;
}

.nav-menu.active {
    display: flex;
    animation: fadeIn 0.3s forwards;
}

.nav-menu a {
    text-decoration: none;
    color: #666;
    padding: 8px 15px;
    margin: 5px;
    transition: all 0.2s;
    font-size: 14px;
}

.nav-menu a:hover {
    color: #333;
    background: #f5f5f5;
}
JavaScript
document.querySelector('.hamburger-menu').addEventListener('click', function() {
    this.classList.toggle('active');
    document.querySelector('.nav-menu').classList.toggle('active');
});
よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!
目次