My menus are working and look correct. The problem is on touch devices (iPad for example) that, once opened (single touch), they don't close. If you are looking through the menus and don't find what you need, you have to refresh to go back to your page. If you touch outside the menu, they stay open. This has come up in user testing as a problem for new users, especially those new to touch devices.

Is there a way to have them close after a set time or if you touch outside the menu? I'm not that good with js, so I'm not even sure where to begin.

I'm using TB_Megamenu 7.x-1.0-beta2+0-dev with a subtheme of Adaptive Themes Corolla. I have applied the following patches to tb_megamenu in case that matters:
#2011042: Menu doesn't open on a tablet
#2053875: Menu items loosing attributes

Thanks in advance for an help you can offer or even confirmation that this is the expected behavior.
Heidi

Comments

mazz0016’s picture

I am having this problem too. Has anyone found a solution to it yet?

Thank you

hwasem’s picture

I haven't found a solution yet, @mazz0016

mazz0016’s picture

@hwasem: I have found a solution. I figured out that there were 2 spans called class=toggle. Both had identical looking parents, however only the second one had the complete structure of the menu. Both spans and their parents were stacked on top of each other. The second span is nested in the first span. When the MENU icon was clicked, the first span and it's parent were targeted and the menu would then expand as expected because the first span was then hidden and the user could then view the menu underneath because the second span (containing the menu) could display. However because there were 2 spans of the same class name and identical looking parents, the code assumed the first span and it's parent were then being targeted again in the javascript when the user clicked the MENU icon again to close. And because the first span was now hidden and the javascript was targeting only the first span, the toggleClass in the javascript wasn’t removing the ‘responsive-toggled’ class, therefore the menu wouldn't close. The second span needed to be targeted in the javascript because it was the one that was open and getting the click event, but ‘responsive-toggled’ class needed to be removed from it’s great grand parent div to close it, not it’s parent.

<div class="parent">
    <span class="toggle">
        <div class="toggle-inner">☰ Menu</div>
    </span>
    <div class="menu">
        <h2><span>Site Menu</span></h2>
                <!--No menu here-->
     
      <div class="parent">
          <span class="toggle">
              <div class="toggle-inner">☰ Menu</div>
          </span>
              <div class="menu">
                  <div class="wet-boew-menubar mb-mega" data-load="menubar">
                      <div>
                  <!-- region: Mega Menu -->
                             <ul>
                                  <li>....</li>
                                  <li>....</li>
                                  <li>....</li>
                                  <li>....</li>
                              </ul>
                         </div>
                       </div>
                    </div>
                 </div>
              </div>
            </div>

responsive_menus_simple.js:

(function ($) {
    /**
    * Handle clicks & toggling the menu.
    */
    var toggler_click = function () {
        if ($(this).parent().parent().hasClass('responsive-menus-simple')) { //If the second span has been opened
            $(this).parent().parent().parent().removeClass('responsive-toggled'); //Remove class on the second span, close the menu
        }
        else {
            $(this).parent().addClass('responsive-toggled'); //Will fire ONCE when menu is clicked the first time. Only applied to 1st span
        }

    };

The way this works is the condition statement evaluates to see if the second span has been opened. The first time it goes through the user has clicked on the first span (The spans were stacked on top of each other only the first span is accessible the first time) and thus the user has not clicked the second span, so the else will run. The first span will be hidden and the second span will be reveled underneath. The menu is now expanded. The second time it goes through the second span is open so the condition is now true and the first scenario is run. Closing the second span. Since the second span is nested in the first span. We must target the great-grandparent to close the menu.

hwasem’s picture

Thanks for the update, @mazz. I am actually using the full TB_Megamenu on the iPad(modified collapsing breakpoints in css), not the collapsed mobile version. Your update will likely help someone else in the future, though. I had to go to Mobile Navigation module for a collapsed menu at the phone viewport and that is working correctly when I open/close with the ☰ button, thankfully.

So following the logic you found in the TB_Megamenu mobile menu, I'm wondering if I might be able to figure out the tablet viewport problem with the full menu. Are you just using Firebug or Inspect Element in your browser to see the HTML code at the smaller size? Or are you using something to help simulate the touch environment? I can't get Safari to install on my Windows 8.1 tablet, but I was able to get Chrome. It isn't having the same problem with menus staying open as the Kindle on iPad (using Chrome even on iPad they stay open). I'm trying to figure out how to troubleshoot a problem I can't replicate in a full-featured browser. I wish I could get the element inspection to work on the iPad.

Is responsive_menus_simple.js a new js file you created to fix this problem?

Thanks,
Heidi

ankit agrawal’s picture

Category: Support request » Bug report
Priority: Normal » Major

The toggle (close state) is still not working specially in the mobile devices, has anybody found a workaround for it?

I guess the solution suggested by the @mazz0016 is for the tb_sirate and that is using the in built tb_mega_menu for rendering the menu, responsive_menus_simple.js seems to be the file of that theme only.

hwasem’s picture

Still looking into this. I found the demo site for tb_sirate and it is not closing menus on iPad landscape either. So it is not unique to me or my theme. Good to know, I guess.

dsrikanth’s picture

Experiencing same issue. For now, I just added a quick jQuery workaround.

(function($) {
Drupal.behaviors.drupaldeveloper = {
attach: function (context) {
$('a.dropdown-toggle').focusout(function() {
$(this).parent('li').removeClass('open');
})
}
}
})(jQuery);

hwasem’s picture

@dsrikanth, is this in your own custom .js file or somewhere else? I'm can't seem to get that to work on iPad in my theme's custom js.

dsrikanth’s picture

Yes, its in my custom js. I am sorry its not working for you. Not sure how helpful this is but I am working on making the menu more keyboard accessible as well (and thus changing my custom jQuery completely). Here is that I have - its not complete but does few things well. Hope this helps!

            $('.nav > li.mega').click(function(event) {
                if(!$(this).hasClass('open')){
                    $(this).addClass('open');
                } else {
                    $(this).removeClass('open');
                }
                $(this).prev('.nav > li.mega').removeClass('open');
                $(this).next('.nav > li.mega').removeClass('open');
            })
            $('.nav > li.mega').focusout(function() {
                $(this).prev('.nav > li.mega').removeClass('open');
                $(this).next('.nav > li.mega').removeClass('open');
            })
hwasem’s picture

@dsrikanth, I'm curious. Are you seeing the problem only on iOS or with other touch devices, too? I tested Kindles with current version (7.x-1.0-beta5 with patch #2011042: Menu doesn't open on a tablet) and it appears to be fine. I only have Kindles and iPads to test on, unfortunately.

I have tried to use your code, but I couldn't get it to work.

Instead, I think this will work for me in my custom theme .js file:

var isTouch = 'ontouchstart' in window && !(/hp-tablet/gi).test(navigator.appVersion);
	if(isTouch){
		$(document).on('touchstart', function(event) {
			 if (!$(event.target).closest('.nav').length) {
				$('.nav > li.mega').removeClass('open');
			}
		});
	}

So far I'm not seeing any negative impact and my Kindles/desktops are still working as expected. Do you see a problem with doing it this way?

Heidi

dsrikanth’s picture

Hmm interesting. I am working on TB Mega menu 7.x-1.0-beta3 on Bootstrap theme 7.x-3.1-beta2 and jQuery 1.9.
And I have tested this on several iOS devices, Android devices and Windows phones as well..

E Johnson’s picture

Hey dsrikanth,

Thanks for the solutions in #7 and #9. It seems to work for me on mobile device.

Also, adding this code (https://www.drupal.org/node/2274909#comment-9071921) helped solve having to hit level-1 menu items twice to expand the sub-nav.

Hope this helps someone in the future. Adding this comment so I can remember this fix for future projects.

neschi’s picture

I've fixed it with this little code, just put it on a custom JS file. I hope you find it useful.

$('.nav > li.mega').click(function(event) {
    if (!$(this).hasClass('open-fix')) {
        $("ul.tb-megamenu-nav li").removeClass("open-fix");
        $(this).addClass('open-fix');
        if (!$(this).hasClass('open')) {
            $(this).addClass('open');
        }
    } else {
        $(this).removeClass('open-fix');
        $(this).removeClass('open');
    }
});

Best regards!

nickap’s picture

#13 worked great, thank you! I had an additional problem though, which was that not only were menus not closing if you clicked again on the same top menu item, but they also would not close if you clicked on a different menu item - so if you click on menu item 1, and the submenu for that item appears, then you click on menu item 2 without first closing submenu1, then submenu1 would stay open and the submenu for item 2 would appear stacked over the top of submenu1. So I just altered the code from #13 very slightly to close all submenus when any top menu item is clicked, then open the submenu for just the clicked item. (Since I'm a JQuery idiot, I'm also including here the 'on document ready' code that is required for the function to work. The code below, copied and pasted directly into JS Injector, solved all menu problems for me on touch devices.)

(function ($) {
 $(document).ready(function() {
   $('.nav > li.mega').click(function(event) {
	   
	 if (!$(this).hasClass('open-fix')) {
		 //MENU IS CLOSED
         $("ul.tb-megamenu-nav li").removeClass("open-fix");
         $("ul.tb-megamenu-nav li").removeClass("open");
         $(this).addClass('open-fix');
         $(this).addClass('open');
    } else {
		//MENU IS OPEN
        $(this).removeClass('open-fix');
        $(this).removeClass('open');
    }
	
   });
 });
})(jQuery);
saifullah.akber’s picture

hwasem you solution worked for me. No problem so far. Thank you :)

saifullah.akber’s picture

sempke’s picture

i have include de js-code from hwasem an also neschi in js injector by drupal.
It doesn't work on IPAD.
The dropdown still not close when you touch the screen somewher!!!

sempke’s picture

if this does not work on iPad, please take TB MEGA MENU off the market from drupal !

caminadaf’s picture

Status: Active » Needs review
StatusFileSize
new3.51 KB

Hello all, I've created a patch for this module to resolve this issue and maintain all of the old functionalities.

Now if a link has a submenu, you can open and close the menu freely.

If you click anywhere outside the meganav, all of the menus are closed.

andrewozone’s picture

Issue tags: +GovCon2020
knaffles’s picture

StatusFileSize
new3.32 KB

I tested the patch in #19, but it didn't work for me for a couple of reasons:

  • While it does let you tap on an open top level menu item to close the dropdown, we don't necessarily want that behavior. Some top level links are linked to pages, so the current functionality is that the first tap opens the menu and the second tap takes you to the link.
  • We need to preserve the above functionality, while also providing the ability to tap away from the menu to close the dropdown. Patch #19 allows for tapping away on most devices but does not work on iOS.

The attached patch provides the ability to tap on another top level menu item, or tap away from the menu (ie, anywhere else on the screen) in order to close the currently open menu item. Testing should be performed on both touch desktops and iOS.

scorbine’s picture

Status: Needs review » Reviewed & tested by the community

  • 5a7e7c1 committed on 7.x-1.x
    Issue #2285435 by caminadaf, knaffles, scorbine: Closing menu on touch...
knaffles’s picture

Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.