jquery - Close the hamburger menu when you click outside of container -
i want have simple hamburger menu. i'm using jquery toggle open , close when hamburger clicked. user able close when click anywhere outside of div or menu.
the jsfiddle have written out. don't mind styling.
i use like
$(window).on('click', function(){ $('.responsive-menu').removeclass('expand'); })
but added, cannot open menu. please help. thank you!
the trick check if element clicked has parent .responsive-menu
or .menu-btn
.
if has'n of 2 , menu expanded, toggle!
$(document).on("click", function(e){ if( $(e.target).closest(".responsive-menu").length == 0 && $(".responsive-menu").hasclass("expand") && $(e.target).closest(".menu-btn").length == 0 ){ $('.responsive-menu').toggleclass('expand'); } });
Comments
Post a Comment