Menu Closing alone

0

I have a menu in javascript and my knowledge in javascript is almost null. And unfortunately this menu on the mobile it is closing alone ... and as I do not have much knowledge I am suffering to solve this problem.

Here's the mobile site and I'll leave the code below.

Menu closing alone: link

Code:

        // Header Menu Mobile Nav
    if (jQuery('.menu')) {
        jQuery('.nav-toggler').click(function (e) {
            e.preventDefault();
            jQuery(this).next('.menu').addClass('open');
            e.stopPropagation();
        });

        jQuery('.menu').on('click', function(e) {
            e.stopPropagation();
        });

        jQuery('body').on('click', function (e) {
            jQuery('.menu').removeClass('open');
        });

        jQuery('#closeMenu').click(function (e) {
            e.preventDefault();
            jQuery('.menu').removeClass('open');
        });
    }
    
asked by anonymous 12.12.2017 / 13:48

2 answers

1

Well, I think that's it, your script starts by checking the " .nav-toggler ", so if it was clicked, it adds the class " .open " to the element with class .menu . script, understand this as a computer processing the script. the next block detects again " .menu ", then detects a click on the element " body " which is the body of the page, if it was clicked it removes the class " .open " from " .menu " , and I think that's your problem. because " .nav-toggler " is inside the body tag, hence the script while adding the class it removes next. is resolved only by removing the block:

jQuery('body').on('click', function (e) {
  jQuery('.menu').removeClass('open');
});

Instead, use another technique to detect the click off the menu: link

    
12.12.2017 / 14:54
0

You do not need to remove anything from the code and keep the click functionality on body to close the menu. Leave your code as is and add% as_with% as below.

Your problem is in if in this part of the code that simulates a click on the page, closing the menu:

// Carousel on Tabs of 'Unidades de Lazer'
tabCarousel = setInterval(function(){
   var active = tabNavItem.filter('.active'),
   next = active.next('li'),
   toClick = next.length ? next.find('a') : tabNavItem.eq(0).find('a');
   toClick.trigger('click');
}, 3500);

Add a trigger to check that the menu is open by preventing it from being closed by itself:

// Carousel on Tabs of 'Unidades de Lazer'
tabCarousel = setInterval(function(){
   var active = tabNavItem.filter('.active'),
   next = active.next('li'),
   toClick = next.length ? next.find('a') : tabNavItem.eq(0).find('a');
   if($('.nav-toggler').hasClass('open')){ // este if
      toClick.trigger('click');
   }
}, 3500);
    
12.12.2017 / 15:09