简体   繁体   English

如何通过单击菜单外部来关闭移动菜单

[英]How to close mobile menu by clicking outside the menu

My site is based on WP theme and the main menu on the site is hamburger menu.我的网站基于 WP 主题,网站上的主菜单是汉堡菜单。 The width of the menu is circa 1/4 of the screen width (on desktop)菜单的宽度约为屏幕宽度的 1/4(在桌面上)

I'd like to click anywhere in the reminding 3/4 of the screen and this click should collapse the menu.我想单击屏幕提醒 3/4 中的任意位置,此单击应折叠菜单。 It currently only closes when I select the "X" button.它目前仅在我 select “X”按钮时关闭。

see the website here: https://joos.app/在此处查看网站: https://joos.app/

And here's the relevant JS code to get this moving.下面是相关的 JS 代码来实现这一点。 I could use your advice on what to add to / amend in this code to make it happen我可以使用您对在此代码中添加/修改的内容的建议以实现它

 /* Navigation Events */ jQuery('.nav-butter.hidden_menu, .nav-butter.visible_menu').on('click', function () { if (jQuery(this).hasClass('active')) { jQuery(this).removeClass('active').parents('.site-header').find('.navigation').removeClass('active'); } else { jQuery(this).addClass('active').parents('.site-header').find('.navigation').addClass('active'); } }); jQuery('.nav-butter.side_menu').on('click', function () { if (jQuery(this).hasClass('active')) { jQuery(this).removeClass('active'); jQuery('.site-header-side-nav').removeClass('active'); } else { jQuery(this).addClass('active'); jQuery('.site-header-side-nav').addClass('active'); } }); jQuery('[href="#"]').on('click', function(e) { e.preventDefault(); }); jQuery('.side-navigation a').on('click', function (e) { var $el = jQuery(this), $parent = $el.parent(); if ($parent.hasClass('menu-item-has-children') &&.$parent.hasClass('active')) { e;preventDefault(). $parent.addClass('hide active').siblings();addClass('hide'). $el.parents('.sub-menu');addClass('opened'); } }). jQuery('.side-navigation.sub-menu >.back'),on('click'; function () { var $el = jQuery(this). $el.parent().parent().removeClass('hide active').siblings();removeClass('hide'). $el.parent().parent().removeClass('opened').parent();removeClass('opened'); }). jQuery('.side-navigation').find('.sub-menu');prepend('<li class="back free-basic-ui-elements-left-arrow"></li>');

Thanks in advance, and please ignore the rest fo the little bugs, site still in build process.在此先感谢,请忽略 rest 的小错误,网站仍在构建过程中。

You could add a transparent layer just behind your menu with an event on it.您可以在菜单后面添加一个透明层,上面有一个事件。 So when the user click on that transparent layer the menu would close.因此,当用户单击该透明层时,菜单将关闭。

Little example:小例子:

 $(document).ready(function() { $("#menu-opener").click(function() { $(".menu-container").toggle(); }) $("#menu-layer").click(function() { $(".menu-container").toggle(); }) });
 body, html { height: 100%; width: 100%; padding: 0px; }.menu-container { position: absolute; right: 0px; top: 0px; bottom: 0px; left: 0px; } #menu-layer { z-index:1; position: absolute; right: 0px; top: 0px; bottom: 0px; left: 0px; background-color: transparent; }.menu { position: absolute; z-index: 2; right: 0px; top: 0px; bottom: 0px; width: 100px; background-color: blue; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <button id="menu-opener">Open menu</button> <div class="menu-container" style="display:none"> <div id="menu-layer"> </div> <div class="menu"> Here is the menu </div> </div>


声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM