DEV Community

Cover image for Easy hamburger menu with JS - Beginners

Easy hamburger menu with JS - Beginners

ljc-dev on July 26, 2020

A hamburger menu is a classic UI feature present in countless websites. It's used to show and hide a menu on click, especially used in mobile desig...
Collapse
 
studmuffin30 profile image
studmuffin30 • Edited

nice article,merci

Collapse
 
ljcdev profile image
ljc-dev

Thx Stud 😄, glad u like it!

Collapse
 
lathryx profile image
Lathryx

Hey! I can't get it working and I'm not really sure why.

The CSS properties are successfully being toggled because the button switches images correctly, and the event listeners are set up correctly because if I put a console.log statement in them that also works.

Does anybody know why?

Collapse
 
dzopunk profile image
dzopunk

I had the same problem.
I'm really not sure why. Tutorial is pretty straight forward.
My solution was creating new class with translateY(-100%) and transition property and adding it on .menu and toggle it in js.

I really wish someone replied to you sooner because I lost so much time 🤣

Collapse
 
ahmadullahnikzad profile image
ahmadullah

Thanks,
I want to create navigation drawer like dev.to's
Because when I click outside of the drawer it disappears.
Can anyone suggest me any solution.

Collapse
 
ljcdev profile image
ljc-dev

Thanks for reading Ahmadullah!! I've recreated this ham menu to make it similar to dev.to's.
codepen.io/ljc-dev/pen/zYoqbrN

Collapse
 
ahmadullahnikzad profile image
ahmadullah

Thanks a lot.
I was searching for this solution but I couldn't find it.
if you have any problem then let me know.
Thanks.

Thread Thread
 
ljcdev profile image
ljc-dev

U're welcome and thx for the offer 😉. I'm currently getting ready to start freelancing. Building projects and going to re design my portfolio 👌.

Collapse
 
lathryx profile image
Lathryx • Edited

testing bug with inline code.

Edit: Could not reproduce...

Collapse
 
ljcdev profile image
ljc-dev

Hey Lathryx, thx for checking my tutorial. I tried my best, but it isn't perfect 😄. If u want explanations on sth, u can find me on twitter 👌.

Collapse
 
ljcdev profile image
ljc-dev

Or ask here. But I meant I'm more present on twitter.

Collapse
 
andresfjobs profile image
andresfjobs

great article man, I appreciated it a lot

Collapse
 
ljcdev profile image
ljc-dev

Thx a lot Andres, glad u found it useful 😁!!

Collapse
 
saltybaguette profile image
Brian Gay

Any ideas on how to disable the menu from appearing out by default when navigating back to the page?

Collapse
 
ljcdev profile image
ljc-dev

Hello Brian, sorry about that delay. I've tried adding an about page and going to and back, the menu didn't stay open 🤔. If you are still having this trouble, I could have a look at your code 👌.

Collapse
 
benjohnsonfullstack profile image
Ben Johnson

Thanks for the article! Saved me on a project that I was doing on my own a little ahead of my current knowledge. A quick note: I ended up using .replace instead of .add/.remove -- a little cleaner and particularly handy for wiping any CSS styles that you might want to completely hide/show when you trigger the toggle.
Image description

Thanks again! Huge help!

Collapse
 
nippermh profile image
Mark Hallam

Thanks for this. I just removed jquery and used the javascript to animate my hamburger icon. Sweet.

Collapse
 
ljcdev profile image
ljc-dev

You're welcome 😀!

Collapse
 
emjogale profile image
Emma

Thanks this was just what I was looking for! Great description 👍

Collapse
 
ljcdev profile image
ljc-dev

Glad it was of help Emma 😃!! I still found it lacking and rewrote it once again 😂.

Collapse
 
keshri95 profile image
Ashish Keshri

amazing

Collapse
 
mihaigabriel1 profile image
mihai-gabriel1

Great work!

Collapse
 
dzopunk profile image
dzopunk • Edited

It was really fun doing this tutorial, and thanks for great step by step explanation 🤠