简体   繁体   English

重叠点击的关闭菜单需要双击而不是单击

[英]Close menu on overlay click requires double click instead of single click

I have a slide out menu that I want to close when the user clicks on the overlay. 我有一个滑出菜单,当用户单击叠加层时,我想关闭它。 The menu closes but to open it again I have to click the toggle twice instead of once, where I'm I going wrong?Thanks. 菜单关闭,但是要再次打开它,我必须单击两次而不是一次,这是我错了吗? A sample page demonstrating this slideoutMenu 演示此SlideoutMenu的示例页面

 function expandOverlay() { var overlay = document.createElement("div"); overlay.setAttribute("id", "overlay04"); overlay.setAttribute("class", "overlay04"); document.body.appendChild(overlay); } function restore() { document.body.removeChild(document.getElementById("overlay04")); } // create menu variables var slideoutMenu = $('.slideout-menu'); var slideoutMenuWidth = $('.slideout-menu').width(); $(document).ready(function() { $('.slideout-menu-toggle').on('click', function(event) { event.preventDefault(); // toggle open class slideoutMenu.toggleClass("open"); // slide menu if (slideoutMenu.hasClass("open")) { slideoutMenu.animate({ left: "0px" }); expandOverlay(); } else { slideoutMenu.animate({ left: -slideoutMenuWidth }, 250); restore(); } }); }); window.addEventListener('mouseup', function(event) { var box = document.getElementById('menu_s'); if (event.target != box && event.target.parentNode != box) { slideoutMenu.animate({ left: -slideoutMenuWidth }, 250); restore(); } }); 
 <nav> <ul> <li><a href="#" class="lag slideout-menu-toggle">open menu</a></li> </ul> </nav> <!--begin slideout menu--> <div id="menu_s" class="slideout-menu"> <h3>Last Week<a href="#" class="slideout-menu-toggle">&times;</a></h3> <div class="fslide"></div> <ul> <li><a href="http://dundaah.com/">Dundaah</a></li> <li><a href="http://pics.dundaah.com/">Pics</a></li> <li><a href="http://blog.dundaah.com/">Blog</a></li> <li><a href="http://music.dundaah.com/">Music</a></li> </ul> </div> 

I believe the problem here is that the class open is still on toggleMenu after the overlay is clicked. 我相信这里的问题是单击叠加层后,打开的类仍在toggleMenu上。

Therefore, if you were to click open menu after closing, the click event would remove the open class from toggleMenu, resulting in the else part of the if statement being executed (which is to close the menu). 因此,如果要在关闭后单击打开菜单,则click事件会从toggleMenu中删除打开类,从而导致if语句的else部分正在执行(即将关闭菜单)。

This is why 2 clicks are needed before the menu will be open if it was closed by clicking the overlay. 这就是为什么如果通过单击覆盖关闭了菜单,则需要两次单击才能打开菜单。

You could possibly try to toggleClass("open") when closing the menu by clicking on the overlay. 在关闭菜单时,您可以尝试通过单击覆盖图来切换toggleClass(“ open”)。

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

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