HomeHTML CSS & JAVASCRIPTNavigation MenuHow To Make A Dropdown Menu Animation In HTML CSS JS

How To Make A Dropdown Menu Animation In HTML CSS JS

How To Make A Dropdown Menu Animation In HTML CSS JS

Hello everyone! In this blog post, we will learn how to make a dropdown menu animation in HTML CSS and JavaScript. Navigating the realms of web development often involves a delicate dance between functionality and aesthetics, and one choreographic element that has taken center stage is the dropdown menu animation. In this blog post, we’ll unravel the intricacies of what makes a dropdown menu animation, exploring its significance, implementation, and the magic that transpires when HTML, CSS, and JavaScript join forces.

Video Tutorial Of Dropdown Menu Animation HTML CSS JavaScript

Demystifying Dropdown Menu

Before delving into the enchanting world of animations, let’s first understand the foundational concept of dropdown menu. At its core, a dropdown menu is a user interface element that presents a list of options when activated. This activation can be triggered by a click or hover action on a designated button, unleashing a cascade of choices like a well-choreographed performance.

The Anatomy of a Dropdown Menu

Typically integrated into navigation bars, dropdown menus serve a dual purpose, organizing information systematically and decluttering the user interface. The menu comprises a trigger element, often a button or a link, and a hidden container holding a list of selectable options. This hidden container elegantly reveals itself, creating a seamless transition between a compact interface and a menu-rich environment.

From Static to Dynamic – Adding Animation

While static dropdown menus serve their purpose efficiently, the introduction of animations elevates the user experience to a whole new level. Animation, in this context, refers to the artful transition that occurs when the dropdown menu unfolds or retreats. It’s the difference between a functional but mundane menu and one that captivates the user’s attention with fluidity and grace.

The Trio – HTML, CSS, and JavaScript

To comprehend dropdown menu animations, it’s imperative to explore the trio responsible for this digital ballet, HTML, CSS, and JavaScript. HTML provides the structural backbone, defining the elements and their relationships. CSS steps in to weave the visual tapestry, styling the dropdown to align with the website’s aesthetics. Finally, JavaScript orchestrates the interactivity, breathing life into the animation.

Source Files – Dropdown Menu Animation HTML CSS JS

Building the Foundation with HTML

Let’s embark on a journey to create a basic dropdown menu using HTML. In this example, a simple structure is established with a div as the trigger and a hidden div for the menu options.

<!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">
    <title>Dropdown Animation | Codehal</title>
    <link rel="stylesheet" href="style.css">
</head>

<body>

    <div class="box">
        <div class="dropdown">Dropdown
            <span class="left-icon"></span>
            <span class="right-icon"></span>
            <div class="items">
                <a href="#" style="--i:1;"><span></span>HTML</a>
                <a href="#" style="--i:2;"><span></span>CSS</a>
                <a href="#" style="--i:3;"><span></span>JavaScript</a>
            </div>
        </div>
    </div>

    <script src="script.js"></script>
</body>

</html>

Styling the Interface & Crafting Animation or Transition with CSS

CSS swoops in to stylize our dropdown, making it visually appealing and seamlessly integrated into the website design. And by adding animation or transition to the dropdown, it will make the dropdown look more attractive, enhancing the visual appeal of the dropdown.

@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@600&display=swap');

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    font-family: 'Poppins', sans-serif;
}

body {
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
    background: #d5d0d6;
}

.box {
    position: relative;
    width: 250px;
    height: 250px;
}

.dropdown {
    position: relative;
    width: 100%;
    height: 60px;
    background: #7f0099;
    color: #fff;
    font-size: 22px;
    display: flex;
    justify-content: center;
    align-items: center;
    border-radius: 5px;
    cursor: pointer;
    box-shadow: 0 5px 10px rgba(0, 0, 0, .2);
}

.dropdown .left-icon,
.dropdown .right-icon {
    position: relative;
    top: 2px;
    display: inline-block;
    width: 15px;
    height: 5px;
    background: #fff;
    border-radius: 40px;
    transition: .5s;
}

.dropdown .left-icon {
    left: 7px;
    transform: rotate(45deg);
}

.dropdown.active .left-icon {
    transform: rotate(135deg);
}

.dropdown .right-icon {
    transform: rotate(-45deg);
}

.dropdown.active .right-icon {
    transform: rotate(-135deg);
}

.dropdown .items {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 160px;
    margin-top: 63px;
    overflow: hidden;
    visibility: hidden;
    transition: .5s;
}

.dropdown.active .items {
    visibility: visible;
}

.dropdown .items a {
    position: relative;
    left: 100%;
    display: flex;
    font-size: 20px;
    background: #fff;
    color: #7f0099;
    text-decoration: none;
    border-radius: 5px;
    padding: 10px 15px;
    margin-top: 2.5px;
    z-index: 1;
    overflow: hidden;
    transition: .5s;
    transition-delay: calc(60ms * var(--i));
}

.dropdown.active .items a {
    left: 0;
}

.dropdown .items a:hover {
    color: #fff;
}

.dropdown .items a span {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: #bb00e0;
    z-index: -1;
    border-radius: inherit;
    transform: rotate(160deg);
    transform-origin: right;
    transition: .5s;
}

.dropdown .items a:hover span {
    transform: rotate(0deg);
}

Controlling with JavaScript

Empower your website dropdown menu with dynamic flair using JavaScript active class manipulation. By seamlessly toggling the active class, you gain precise control over the menu visibility and behavior. This method enhances user experience, allowing for smooth transitions and interactive responsiveness. Unlock the potential of your navigation with this efficient and user-friendly approach, injecting vitality into your website design.

const dropdown = document.querySelector('.dropdown');

dropdown.addEventListener('click', () => {
    dropdown.classList.toggle('active');
});

Conclusion

In conclusion, dropdown menu animations encapsulate the harmony of form and function in web development. The collaborative efforts of HTML, CSS, and JavaScript empower developers to create menus that transcend the ordinary, providing an interactive and visually enchanting user experience. As you embark on your coding endeavors, let these animations be the choreography that breathes life into your website. That’s the explanation regarding how to make a dropdown menu animation in html css and javascript. Happy coding!

RELATED TUTORIALS

Most Popular

CATEGORIES