CSS过渡仅以一种方式起作用

想要在切换菜单打开/关闭时添加不透明度过渡。它在菜单打开时起作用,但在菜单关闭时不起作用。为什么会这样呢?

// Selectors
let header = document.querySelector('.header')
let hamburgerMenu = document.querySelector('.hamburger-menu')

hamburgerMenu.addEventListener('click', function() {
    header.classList.toggle('menu-open');
})
/* Basic reset */
*,
*::before,
*::after {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

/* Custom properties */
:root {
    --dark-color: #2d2c2c;
    --purple-solid: #350a4f;
    --purple-transparent: rgba(53, 10, 79, .7);
    --purple-transparent-alt: rgba(53, 10, 79, .5);
    --purple-light: #8f50fb;
    --yellow-solid: #fa9e2c;
    --gradient-color: linear-gradient(to right, var(--yellow-solid), var(--purple-light));
    --gradient-color-alt: linear-gradient(to right, var(--purple-light), var(--yellow-solid));
}

/* Global styles */
html {
    font-size: 10px;
}

body {
    font-family: 'Open Sans', sans-serif;
    font-size: 1.6rem;
    color: var(--dark-color);
}

a {
    text-decoration: none;
    color: inherit;
}

ul {
    list-style: none;
}

section {
    padding: 5rem 0;
}

/* Reusable styles */
.container {
    width: 100%;
    max-width: 120rem;
    padding: 0 1.5rem;
    margin: 0 auto;
}

/*
.header
    .container
        .nav
            .logo
                img
            .hamburger-menu
                i
            .nav-list
                .nav-item
                .nav-link
*/

/* Header styles */

.header {
    background-color: var(--purple-transparent);
    width: 100%;
    height: 6rem;
    display: flex;
    align-items: center;
    position: fixed;
    top: 0;
    left: 0;
    z-index: 1000;
}

/* Header styles - nav */
.nav {
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.hamburger-menu {
    font-size: 2.6rem;
    color: #fff;
    cursor: pointer;
    position: relative;
    z-index: 1500;
}

.hamburger-menu .fa-times {
    display: none;
}

.menu-open .nav-list {
    opacity: 100%;
    transform: scale(1);
    transition: opacity .5s;
}

.menu-open .hamburger-menu .fa-times {
    display: block;
}

.menu-open .hamburger-menu .fa-bars {
    display: none;
}

.nav-list {
    background-color: var(--purple-solid);
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100vh;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    z-index: 1400;
    opacity: 0;
    transform: scale(0);
    transition: opacity .5s;
}

/*
.header
    .container
        .nav
            .logo
                img
            .hamburger-menu
                i
            .nav-list
                .nav-item
                .nav-link
*/

.nav-item:not(last-child) {
    margin-bottom: 4rem;
}

.nav-link {
    display: block; /* allows us to use margin & paddings - by default they're inline elements and so no margin / padding */
    color: #fff;
    font-size: 3rem;
    text-transform: uppercase;
    letter-spacing: 2px;
    cursor: pointer;
    padding: 1rem;
}
<header class="header">
    <div class="container">
        <nav class="nav">
            <div class="hamburger-menu">
                <i class="fas fa-bars"></i>
                <i class="fas fa-times"></i>
            </div>
            <ul class="nav-list">
                <li class="nav-item">
                    <a href="index.html" class="nav-link">Home</a>
                </li>
                <li class="nav-item">
                    <a href="#" class="nav-link">About</a>
                </li>
                <li class="nav-item">
                    <a href="#" class="nav-link">Offers</a>
                </li>
                <li class="nav-item">
                    <a href="#" class="nav-link">News</a>
                </li>
                <li class="nav-item">
                    <a href="#" class="nav-link">Contact</a>
                </li>
            </ul>
        </nav>
    </div>
</header>
评论