JQuery: click the SlideUp Issue button

I have a jQuery navigation system which when I click on the parent <li> slides using jQuery. A second press will move the navigation up. The problem is that my clients want him to also make a backup, if he clicks elsewhere on the screen, that is, just clicking on an empty space will crash the navigator.

Any ideas?

Here is the jQuery:

 $('#menu-flag-menu > li > a').click(function(e) {e.preventDefault();}); $("#menu-flag-menu li a").click(function(){ // close all opened sub menus $("ul", $(this).parent().siblings()).stop(true,true).slideUp(); var ul = $(this).next("ul"); ul.stop(true,true).slideToggle('fast'); }); 
+4
source share
2 answers

This should work:

 $(document).click(function (e) { // if we click on anything other than the navigation menu // or it descendants then slide up the menu if(e.target.id !== "menu-flag-menu" && !$(e.target).closest("#menu-flag-menu").length) { $(".sub-menu").stop().slideUp(); } }); 
+3
source

My advice is to try using the .mouseenter event to open the menu and the mouseleave event to close the menu.

0
source

Source: https://habr.com/ru/post/1390244/


All Articles