Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

HTML & CSS How to animate navbar opening/closing on mobile?

winstonp

New Coder
I adapted an online navbar implementation which collapses and expands on mobile, and now I would like to animate the navbar opening and closing.

Here's the HTML:

HTML:
<nav>
<div id="nav-wrapper">
<button class="icon" onclick="menuBar()"><i class="gg-menu-right"></i></button>
<button onclick="darkMode()"><i class="gg-dark-mode"></i></button>
<a href="/pages/index.html">Winston Purnomo</a>
<a href="/pages/portfolio.html">Portfolio</a>
<a href="/pages/contact.html">Contact</a>
</div>
</nav>`

And here's the existing relevant CSS:

CSS:
/* -- Navigation Bar. -- */
nav {
    overflow: hidden;
    position: sticky;
    top: 0px;
    background-color: #333;
    padding: 20px 20px;
    font-size: var(--nav-text);
}
....
@media screen and (max-width: 600px) {
    ....
    nav.responsive {
        position: relative;
    }
    nav.responsive .icon {
        position: sticky;
        right: 0;
        top: 0;
    }
    nav.responsive a {
        float: none;
        display: block;
        text-align: left;
    }
}
 
Can I see the function that opens/closes your navbar?
Then I can see how to help you animate it. 🙂
Yup!

JavaScript:
function menuBar() {
    var x = document.querySelector('nav');
    if (x.className === 'nav') {
      x.className += " responsive";
    } else {
      x.className = 'nav';
    }

I've done some further reflection on my code, and I think this is why it's hard to animate.

The nav does not change height or position on its own. The nav's 'a' links unhide themselves through pressing the menu button, which causes the navbar to expand in height. How can I tell the CSS to animate this change?
 
Last edited:
I think adding transition: 0.5s; to the nav's CSS should do the trick. (Change 0.5s to the duration you want the animation to be)
 
Last edited:

Latest posts

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom