Skip to content
Advertisement

hide sidebar when click anywhere in page

I have an animate sidebar which appears when user clicks on a hamburger button. Here is the structure :

JavaScript
JavaScript
JavaScript

Actually we can open menu by clicking on #nav-toggle element and close it by clicking on this element too. I’d like to allow user to close this menu by clicking anywhere in the page. How can I do do that? I tried with e.preventDefault(); in my if statement but it doesn’t work.

Thanks!

Advertisement

Answer

I suggest to use toggleClass method and animate it by adding transition: .2s to your .menu, working example:

JavaScript
JavaScript
JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement