Home > Software engineering >  How can i make the menu show up with an animation, sliding from the top to it's place?
How can i make the menu show up with an animation, sliding from the top to it's place?

Time:10-31

I don't have an idea of how am I supposed to make the menu show with an animation. I want it to slide in down and back up on toggle. I'm down for any kind of solution.

const toggleButton = document.getElementById('toggleButton');
const naviList = document.getElementById('navi-lista');

toggleButton.addEventListener('click', () => {
    naviList.classList.toggle('active');
})
* {
    font-family: 'Lato', sans-serif;
    box-sizing: border-box;
    margin: 0px;
    padding: 0px;
}

body {
    background-color: #856547;
}

.rodzicnav {
    position: fixed;
    top: 0;
    left: 0;
    margin: auto;
    width: 100%;
}

.navbar {
    background-color: rgb(31, 31, 31);
    align-items: center;
    color: white;
    display: flex;
    justify-content: space-around;
    font-size: 18px;
}


.primary-icon {
    height: 30px;
}

.lista-nav {
    list-style-type: none;
}

.lista-nav .elementlisty {
    display: inline-block;
    padding: 20px;
}

.navbar a {
    color: white;
    text-decoration: none;
}

.grow { 
    transition: all .2s ease-in-out; 
}
    
.grow:hover { 
    transform: scale(1.1); 
}

.menu {
    display: none;
}

.menu-line {
    width: 25px;
    height: 3px;
    background-color: white;
    margin-bottom: 4px;
}

@media all and (min-width: 480px) {
    
    .navbar {
        flex-direction: column;
    }

    .menu {
        display: block;
        position: absolute;
        right: 15px;
        top: 15px;
        cursor: pointer;
    }

    .lista-nav .elementlisty {
        display: block;
        padding: 10px;
        border-top: solid 1px white;
    }

    .lista-nav {
        list-style-type: none;
        width: 100%;
        text-align: center;
        padding-top: 15px;
        display: none;
    }
    
    .logo-kontener {
        width: 100%;
    }

    .primary-icon {
        margin-left: 10px;
        margin-top: 10px;
    }

    .active {
        display: block;
    }

}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <!-- FONT  -->
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Lato:wght@300&display=swap" rel="stylesheet">
    <!--       -->

    <link href="https://unpkg.com/[email protected]/dist/aos.css" rel="stylesheet">
    <link rel="stylesheet" href="style.css">
    
    <title></title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.11.3/gsap.min.js" defer></script>
    <script src="script.js" defer></script>
</head>
<body>

<div >
    <nav >
        <div >
            <a href=""><img  src="obrazy/logo.png" alt=""></a>
        </div>
        <ul  id="navi-lista">
            <li >
                <a href="">O nas</a>
            </li>
            <li >
                <a href="">Realizacje</a>
            </li>
            <li >
                <a href="">Kontakt</a>
            </li>
        </ul>
        <div  id="toggleButton">
            <div ></div>
            <div ></div>
            <div ></div>
        </div>
    </nav>
</div>

</body>
</html>

I tried watching youtube tutorials, look for some anwser at freecodecamp and w3s. I'm also kind of new to responsive website designs, so I appreciate your help.

CodePudding user response:

You can try to use max-height as property to animate:

@media all and (max-width: 480px) {

.navbar {
    position: relative;
    padding: 10px 15px;
}

.menu {
    display: block;
    cursor: pointer;
}

.lista-nav .elementlisty {
    display: block;
    padding: 10px;
    border-top: solid 1px white;
}

.lista-nav {
    transition: max-height .3s;
    list-style-type: none;
    width: 100%;
    text-align: center;
    position: absolute;
    top: 100%;
    left: 0;
    right: 0;
    background-color: rgb(31, 31, 31);
    max-height: 0;
    overflow: hidden;
}

.logo-kontener {
    width: 100%;
}

.primary-icon {
    margin-left: 10px;
    margin-top: 10px;
}

.active {
    max-height: 500px;
}

}

  • Related