对象已移动

可在此处找到该文档 Animated Hamburger Menu [Article] | Treehouse Blog – New Self New Life
New Self New Life
No Result
View All Result
  • Home
  • Entertainment
  • Celebrity
  • Cinema
  • Music
  • Digital Lifestyle
  • Social Media
  • Softwares
  • Devices
  • Home
  • Entertainment
  • Celebrity
  • Cinema
  • Music
  • Digital Lifestyle
  • Social Media
  • Softwares
  • Devices
New Self New Life
No Result
View All Result
Home Softwares

Animated Hamburger Menu [Article] | Treehouse Blog

by admin
3 years ago
in Softwares
Animated Hamburger Menu [Article] | Treehouse Blog
Share on FacebookShare on Twitter


Hamburger menus are nice for toggling a cellular navigation. Including slick animations to indicate the open or closed state might be tough however I’ll clarify how this may be finished fairly simply. Let’s get began. ?

If video tutorials are extra your factor:

In case you’d wish to see a demo of this earlier than we get began, you’ll be able to see this in motion at my codepen:

Animated Hamburger Menu Codepen

We’ll have to create a number of recordsdata to get began: index.html, types.css, & app.js.

HTML

In our index.html file, lets setup a navigation with <nav></nav>.

Inside our nav tag, we’ll create a container for our hamburger menu. This can simply be a div with the category of hamburger-menu.

<nav>
    <div class="hamburger-menu"></div>
</nav>

Subsequent, we’ll add in every of the three hamburger menu ‘bars’. These might be divs with the category of ‘ham-bar’ however they may every get a novel class title as effectively. That is the code I’ll be working with:

<nav>
    <div class="hamburger-menu">
        <div class="ham-bar bar-top"></div>
        <div class="ham-bar bar-mid"></div>
        <div class="ham-bar bar-bottom"></div>
    </div>
</nav>

That ought to be it for our HTML, so now let’s sort out the CSS.

CSS

We will get began with some fundamental types and resets:

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

physique {
    background-color: #ccc;
}

As soon as that’s finished, lets add some fast stylings to the nav.

nav {
    padding: 1rem;
    background-color: #222;

    show: flex;
    justify-content: flex-end;
    align-items: middle;
}

For the reason that solely youngster factor of our nav is the <div class="hamburger-menu"></div> we are able to use flex to justify the content material to flex-end in order that our hamburger menu is all the way in which to the proper.

Subsequent, we are able to begin styling our precise hamburger menu.

.hamburger-menu {
    top: 50px;
    width: 50px;
    place: relative;
    cursor: pointer;
    padding: 1rem;
}

We place this relative since its youngster parts (<div class="ham-bar"></div> * 3) will must be positioned completely.

All the <div class="ham-bar"></div> parts will get the next types:

.ham-bar {
    width: 70%;
    top: 4px;
    background-color: white;
    border-radius: 25px;
    place: absolute;
    prime: 50%;
    left: 50%;
    rework: translate(-50%, -50%);
    transition: rework .6s, opacity .8s, prime .6s;
}

You’ll discover you’ll be able to solely see one ham-bar now. Truly, all three are seen. They’re simply all completely positioned to the middle of .hamburger-menu. So let’s alter the bar-top and bar-bottom:

.bar-top {
    prime: 25%;
}

.bar-bottom {
    prime: 75%;
}

Now you must see your hamburger menu! Subsequent, we’ll wish to hop into the HTML and add a category of lively to our <div class="hamburger-menu"></div>. It is because once we click on on our hamburger menu, we’ll wish to toggle this class on and off.

When the hamburger menu has a category of lively, we’ll wish to model the <div class="ham-bar"></div> appropriately. Lets try this now.

For bar-top we wish to middle it and rotate it:

.hamburger-menu.lively .bar-top {
    rework: translate(-50%, -50%) rotate(-315deg);
    prime: 50%;
}

for bar-mid we wish to set the opacity to 0:

.hamburger-menu.lively .bar-mid {
    opacity: 0;
}

for bar-bottom we wish to middle it and rotate it:

.hamburger-menu.lively .bar-bottom {
    rework: translate(-50%, -50%) rotate(-225deg);
    prime: 50%;
}

Now go forward and add a category of lively to your hamburger-menu within the HTML to verify your hamburger-menu resembles and x. If it does, nice! If not, make sure that to re-read the above and determine the place you’ve gone incorrect. As soon as that’s finished, let’s transfer on to toggling this lively class by way of JavaScript!

JavaScript

The JavaScript may be very easy, we’ll simply wish to add an occasion listener on <div class="hamburger-menu"></div>. We will create a variable for this as effectively:

const hamMenu = doc.querySelector('.hamburger-menu');
hamMenu.addEventListener('click on', () => {

});

All we wish to do when this occasion is triggered, is toggle a category of lively to <div class="hamburger-menu"></div>:

const hamMenu = doc.querySelector('.hamburger-menu');
hamMenu.addEventListener('click on', () => {
    hamMenu.classList.toggle('lively');
});

And that’s it, your hamburger menu ought to toggle each the open and closed state when clicked!



Source link

Tags: AnimatedArticleBlogHamburgerMenuTreehouse
Previous Post

Celebs That Grew Up In Random Small Towns And Are Definitely Not Nepotism Babies

Next Post

Charlotte Instructs George to Bow at the Queen’s Funeral

Related Posts

How agile is your crypto? Interview study explores opportunities and challenges of cryptographic update processes
Softwares

How agile is your crypto? Interview study explores opportunities and challenges of cryptographic update processes

by admin
August 12, 2025
This week in AI dev tools: GPT-5, Claude Opus 4.1, and more (August 8, 2025)
Softwares

This week in AI dev tools: GPT-5, Claude Opus 4.1, and more (August 8, 2025)

by admin
August 9, 2025
How Agentic AI Powers Webkul eCommerce Marketplaces
Softwares

How Agentic AI Powers Webkul eCommerce Marketplaces

by admin
August 11, 2025
RubyGems malware campaign steals passwords
Softwares

RubyGems malware campaign steals passwords

by admin
August 8, 2025
Will AI Replace Software Engineers? Detailed Overview
Softwares

Will AI Replace Software Engineers? Detailed Overview

by admin
August 6, 2025
Next Post
Charlotte Instructs George to Bow at the Queen’s Funeral

Charlotte Instructs George to Bow at the Queen's Funeral

The Basics of SEO in 2022 [Infographic]

The Basics of SEO in 2022 [Infographic]

  • Trending
  • Comments
  • Latest
More than 400 Canadian artists sign letter denouncing ‘anti-trans’ policies

More than 400 Canadian artists sign letter denouncing ‘anti-trans’ policies

April 1, 2024
Anne Heche to be taken off life support after compatible organ recipient found – National

Anne Heche to be taken off life support after compatible organ recipient found – National

August 15, 2022
Elmo accidently triggers avalanche of dread with one innocent question – National

Elmo accidently triggers avalanche of dread with one innocent question – National

January 31, 2024
Mike ‘The Situation’ Sorrentino saves 2-year-old son from choking in home video – National

Mike ‘The Situation’ Sorrentino saves 2-year-old son from choking in home video – National

February 5, 2024
Taylor Swift’s ‘Eras Tour’ movie: How and when you can stream in Canada – National

Taylor Swift’s ‘Eras Tour’ movie: How and when you can stream in Canada – National

November 27, 2023
Jacklyn Zeman, longtime ‘General Hospital’ actor, dies at 70 – National

Jacklyn Zeman, longtime ‘General Hospital’ actor, dies at 70 – National

May 11, 2023
Greyson Chance says Ellen DeGeneres ‘abandoned’ him, calls her ‘manipulative’ and ‘opportunistic’ – National

Greyson Chance says Ellen DeGeneres ‘abandoned’ him, calls her ‘manipulative’ and ‘opportunistic’ – National

September 26, 2022
R. Kelly hospitalized after prison overdose, his lawyers allege murder plot – National

R. Kelly hospitalized after prison overdose, his lawyers allege murder plot – National

June 18, 2025
Chance The Rapper Opens Up About Co-Parenting After Divorce

Chance The Rapper Opens Up About Co-Parenting After Divorce

August 12, 2025
Samsung Galaxy S26 Ultra: 6 Key Updates Revealed

Samsung Galaxy S26 Ultra: 6 Key Updates Revealed

August 12, 2025
Taylor Swift new album Easter Eggs: All the TS12 hints we missed leading up to The Life of a Showgirl announcement over the years including a ‘showgirl’ clue back in 2022 with Dita Von Teese

Taylor Swift new album Easter Eggs: All the TS12 hints we missed leading up to The Life of a Showgirl announcement over the years including a ‘showgirl’ clue back in 2022 with Dita Von Teese

August 12, 2025
Living with the Coway Bidetmega 500S: Comfort, Hygiene, and Daily Luxury

Living with the Coway Bidetmega 500S: Comfort, Hygiene, and Daily Luxury

August 12, 2025
LinkedIn Shares Strategic Insights for B2B Marketers [Infographic]

LinkedIn Shares Strategic Insights for B2B Marketers [Infographic]

August 12, 2025
August 8-10 Box Office Recap – ‘Weapons’ debuts with a fantastic $43.5M domestically and $71.8M worldwide, and getting an "A–" on CinemaScore, rare for a horror. ‘Freakier Friday’ debuts with a solid $28.5M domestically and $43.8M worldwide. ‘Fantastic Four’ crosses $400M, but it’s fading quickly.

August 8-10 Box Office Recap – ‘Weapons’ debuts with a fantastic $43.5M domestically and $71.8M worldwide, and getting an "A–" on CinemaScore, rare for a horror. ‘Freakier Friday’ debuts with a solid $28.5M domestically and $43.8M worldwide. ‘Fantastic Four’ crosses $400M, but it’s fading quickly.

August 12, 2025
Prince Harry, Meghan Markle extend multi-year Netflix deal – National

Prince Harry, Meghan Markle extend multi-year Netflix deal – National

August 12, 2025
How agile is your crypto? Interview study explores opportunities and challenges of cryptographic update processes

How agile is your crypto? Interview study explores opportunities and challenges of cryptographic update processes

August 12, 2025
New Self New Life

Your source for entertainment news, celebrities, celebrity news, and Music, Cinema, Digital Lifestyle and Social Media and More !

Categories

  • Celebrity
  • Cinema
  • Devices
  • Digital Lifestyle
  • Entertainment
  • Music
  • Social Media
  • Softwares
  • Uncategorized

Recent Posts

  • Chance The Rapper Opens Up About Co-Parenting After Divorce
  • Samsung Galaxy S26 Ultra: 6 Key Updates Revealed
  • Taylor Swift new album Easter Eggs: All the TS12 hints we missed leading up to The Life of a Showgirl announcement over the years including a ‘showgirl’ clue back in 2022 with Dita Von Teese
  • Home
  • Disclaimer
  • DMCA
  • Privacy Policy
  • Cookie Privacy Policy
  • Terms and Conditions
  • Contact us

Copyright © 2021 New Self New Life.
New Self New Life is not responsible for the content of external sites. slotsfree  creator solana token

No Result
View All Result
  • Home
  • Entertainment
  • Celebrity
  • Cinema
  • Music
  • Digital Lifestyle
  • Social Media
  • Softwares
  • Devices

Copyright © 2021 New Self New Life.
New Self New Life is not responsible for the content of external sites.

New Self New Life