- This topic has 7 replies, 4 voices, and was last updated 3 years, 4 months ago by David.
-
AuthorPosts
-
December 2, 2020 at 9:54 pm #1564975Mikko
Hi,
My mobile menu doesn’t close when clicked/tapped outside of it.
I would like it to do so.
If I have a sub-menu open and I click outside of the whole menu then the sub-menu closes but rest of the menu stays open.
On desktop the menu closes when I tap outside of the menu. On mobile the menu stays open when I tap outside.
How could I get the mobile menu close when tapped outside?
I tried setting the menu open on hover, click menu item and arrow but that didn’t have effect on closing the menu.
Thank you very much
December 3, 2020 at 3:48 am #1565411DavidStaffCustomer SupportHi there,
could you link me to your site, so i can see the current setup ?
December 3, 2020 at 5:45 am #1565573MikkoI attached two examples in private information section.
December 3, 2020 at 8:56 am #1566085LeoStaffCustomer SupportAny chance you can try the off canvas panel for mobile?
https://docs.generatepress.com/article/off-canvas-panel-overview/It should have the behaviour you are after – you can test this on this site ๐
December 4, 2020 at 8:27 am #1567478MikkoYes, the off canvas panel does close when tapped outside.
I’d prefer the traditional menu though that opens downwards. Sometimes I’m not working with Pro so it would be nice to have this effect nonetheless. And all this adds to workflows even though not much but I defenitely would prefer if it was possible to close the menu by tapping outside with all the menus, at least the primary menu as that’s the one I usually use out of the box. I’d prefer if I could have some code to put in my child theme.
Thank you
December 4, 2020 at 12:32 pm #1567805TomLead DeveloperLead DeveloperHi there,
This would require custom javascript. For example:
document.addEventListener( 'click', function( evt ) { var openedMenu = openedMenu = document.querySelector( '.toggled .menu-toggle' ), targetElement = evt.target; // clicked element do { if ( targetElement == openedMenu ) { // This is a click inside. Do nothing, just return. return; } // Go up the DOM targetElement = targetElement.parentNode; } while ( targetElement ); // This is a click outside. openedMenu.click(); } );
Not 100% sure that will work, but it should in theory ๐
December 4, 2020 at 7:00 pm #1568006MikkoThanks Tom,
That code worked.
I just realized that adding JavaScript to child theme is not as straightforwards as putting it inside script tags.
Is there some easy trick to include it in child theme? Otherwise I guess I just keep it in GP element.
Thank you
December 5, 2020 at 4:28 am #1568268DavidStaffCustomer SupportHi there,
you would need to enqueue the scripts in a child theme – this developer level article explains:
https://developer.wordpress.org/themes/basics/including-css-javascript/#scripts
-
AuthorPosts
- You must be logged in to reply to this topic.