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

时间:2020-05-23 17:21:04

标签: javascript css

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

Receiver 126: 3,
Receiver 133: 1 
// 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;
}

1 个答案:

答案 0 :(得分:1)

可能不是您想要的答案,但希望对您有所帮助。

通过删除transform: scale(1);(和transform: scale(0);),过渡可以顺利进行。真的需要这种转变吗?

尝试:

.menu-open .nav-list {
    opacity: 100%;
    transition: all .5s ease-in-out;
    visibility: visible;
}


.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;
    transition: all .5s ease-in-out;
    visibility: hidden;
}