Logo

dev-resources.site

for different kinds of informations.

๐Ÿš€ Day 5: Creating a Hamburger Menu with GSAP

Published at
12/13/2024
Categories
gsap
javascript
beginners
tutorial
Author
anticoder03
Author
11 person written this
anticoder03
open
๐Ÿš€ Day 5: Creating a Hamburger Menu with GSAP

Today, I explored how to create an interactive hamburger menu using GSAP (GreenSock Animation Platform). Hamburger menus are a staple in web design, especially for responsive layouts, and adding animation takes them to the next level!

Letโ€™s dive into what I learned and built. ๐ŸŽ‰


โœจ Project Overview

This project focuses on a menu that slides in from the right when the hamburger icon is clicked and slides back out when the close button is clicked. Using GSAP's timeline, I achieved smooth animations with intuitive controls.


๐Ÿ–ฅ๏ธ Code Breakdown

Hereโ€™s the magic behind the scenes:

JavaScript (Animation Logic)

var menu = document.querySelector("#nav i");
var close = document.querySelector("#full i");
var tl = gsap.timeline();

// Slide-in animation for the menu
tl.to("#full", {
    right: 0,
    duration: 0.5,
});

// Animate the menu items
tl.from("#full h4", {
    x: 100,
    duration: 0.5,
    opacity: 0,
    stagger: 0.25,
});

// Animate the close icon
tl.from("#full i", {
    opacity: 0,
    stagger: 0.5,
});

// Pause the timeline initially
tl.pause();

// Play animation on menu icon click
menu.addEventListener("click", function () {
    tl.play();
});

// Reverse animation on close icon click
close.addEventListener("click", function () {
    tl.reverse();
});
Enter fullscreen mode Exit fullscreen mode

HTML Structure

The HTML defines a simple layout for the main content, the hamburger menu, and the sliding menu:

<div id="main">
    <div id="nav">
        <h2 class="poppins-semibold">Ashish</h2>
        <i class="ri-menu-line"></i>
    </div>
    <div id="full">
        <h4 class="poppins-medium">Home</h4>
        <h4 class="poppins-medium">About</h4>
        <h4 class="poppins-medium">Music</h4>
        <h4 class="poppins-medium">Playlist</h4>
        <h4 class="poppins-medium">Liked</h4>
        <i class="ri-close-line"></i>
    </div>
    <section class="main_sec">
        <h1 class="poppins-semibold song_title">Songs That You Like</h1>
        <p class="poppins-regular">Lorem ipsum dolor sit amet consectetur adipisicing elit...</p>
        <div class="button_container">
            <button class="btn btn-1">Listen</button>
            <button class="btn btn-2">Read More</button>
        </div>
    </section>
</div>
Enter fullscreen mode Exit fullscreen mode

CSS for Styling

The CSS sets the stage for the animation with a modern and clean design. The menu starts off-screen and smoothly slides in.


๐ŸŒŸ Features and Effects

  1. Smooth Slide-in Menu

    • The menu slides in from the right with a duration of 0.5s.
  2. Staggered Menu Items

    • Each menu item appears one after another with a 0.25s delay for an elegant effect.
  3. Reverse Animation

    • Clicking the close button reverses the animation, making it seamless and interactive.
  4. GSAP Timeline Control

    • Using GSAP's timeline, we controlled the order and timing of the animations efficiently.

๐ŸŽฅ Live Demo

Check out the live demo here!


๐ŸŒ GitHub Repository

Find the source code on GitHub.


๐Ÿšง Challenges Faced

  1. Menu Positioning: Ensuring the menu starts off-screen without affecting other elements.
  2. GSAP Timeline Management: Managing multiple animations in a single timeline required some trial and error.

๐Ÿš€ What can improve

  • Adding hover animations for menu items.
  • Enhancing accessibility by implementing keyboard navigation support.
  • Exploring advanced GSAP plugins like ScrollTrigger for scroll-based interactions.

Creating this hamburger menu was both fun and enlightening! Let me know what you think, and stay tuned for more GSAP adventures. ๐ŸŒŸ

gsap Article's
30 articles in total
Favicon
How to improve the Frontend part of the project using one button as an example :))))
Favicon
Reflections on My Coding Journey: Lessons from Recent Weeks
Favicon
Circle Cursor Js #GSAP
Favicon
"Day 7 with GSAP: Interactive Scrolling Animation with Rotating Arrows"
Favicon
GSAP ScrollTrigger animation for Upwork Client Demo
Favicon
Day 6: Text Animation with GSAP โ€“ A Split Letter Magic! ๐ŸŽจโœจ
Favicon
๐Ÿš€ Day 5: Creating a Hamburger Menu with GSAP
Favicon
Day 4: Enhancing UX with a Custom Cursor Using GSAP ๐Ÿš€๐ŸŽจ
Favicon
"Unlocking the Power of GSAP for Stunning Web Animations" ๐ŸŽจโœจ
Favicon
Preloader Animation #GSAP
Favicon
GSAP: GreenSock Animation Platform ๐Ÿš€
Favicon
Animated Carousel
Favicon
๐Ÿš€ ๐—ง๐—ต๐—ฒ๐—ฅ๐—ฒ๐—ฎ๐—น๐—›๐—ผ๐˜๐—ฒ๐—น๐˜€ ๐ŸŒŸ
Favicon
Clip-Path Circle Reveal Animation With Mouse Movement
Favicon
Getting started with Gsap!
Favicon
Todayโ€™s new knowledge #7(GSAP)
Favicon
Image Reveal Animation with HTML, CSS, and GSAP
Favicon
Easy React Animation with GSAP
Favicon
MoonAlert A CSS Animated Moon Notification Box
Favicon
Creating a Mesmerizing Moonlit Night Sky Animation with CSS
Favicon
๐Ÿš€ Motion Scape With GSAP
Favicon
Pinning Images with GSAP: A Smooth Scrolling Animation in Next.js
Favicon
Learning GSAP with a Day-to-Night Scroll Animation ๐ŸŒž๐ŸŒœ
Favicon
From Static to Stunning: Animate with GSAP
Favicon
Blurry Card with Parallax Effect ๐Ÿ’ณ
Favicon
Fun Animation using GSAP
Favicon
Future Bank Fintech Green: The Intersection of Finance and Sustainabilit
Favicon
Framer ground.
Favicon
Parallax image scroll speed with Nuxt 3 and GSAP ๐Ÿ›ฃ๏ธโœจ
Favicon
Creating Interactive Hover Effects with GSAP and SplitType

Featured ones: