简体   繁体   English

Javascript、CSS、HTML - 单击屏幕使下拉菜单消失

[英]Javascript, CSS, HTML - Click screen to make dropdown menu disappear

In my project, I have a button where users can click and a drop down menu appears.在我的项目中,我有一个按钮,用户可以在其中单击并出现一个下拉菜单。 Now I want to implement that when I click anywhere inside the window, the drop down box disappears.现在我想实现当我点击窗口内的任何地方时,下拉框消失。 Does anybody know how to implement this functionality?有人知道如何实现这个功能吗? Thank you.谢谢你。

Here is my code : HTML :这是我的代码HTML

<img onclick="hamburgerIconFunction()" width="43px" class="hamburger-icon" src="{% static 'hamburgericon.png' %}" alt="Image of a hamburger icon">
<div class="hamburgerBox-container">
       <img class="hamburgerBox" src="{% static 'hamburgerBox.png' %}" alt="Image of a navbar popup box">
</div>

CSS: CSS:

.hamburger-icon {
    position: absolute;
    display: none;
    right: 20px;
    bottom:16px;
}

.hamburgerBox {
    display: none;
    position: absolute;
    right: 26px;
    top: 58px;
}


@media screen and (max-width: 991px) {
    .nav-btn {
        display: none;
    }

    .hamburger-icon {
        display: inline;
    }
}

@media screen and (min-width: 992px) {
    .hamburgerBox {
        width: 0px;
        height: 0px;
    }
}

JS: JS:

  function hamburgerIconFunction() {
    hamburgerBox = document.getElementsByClassName("hamburgerBox") [0];
    hamburgerBox.style.display = 'inline';
  }

I think that the blur event could give you the behavior you are looking for.我认为模糊事件可以给你你正在寻找的行为。 When you click the icon, it will have focus.当您单击该图标时,它将具有焦点。 When you click anywhere else, it will lose focus, initiating the blur event.当您单击其他任何地方时,它将失去焦点,启动blur事件。 For the current way you are doing things, something like this would maybe work:对于你目前做事的方式,这样的事情可能会奏效:

const hamburger = document.querySelector('.hamburger-icon');
hamburger.addEventListener('blur', () => {
  document.querySelector('.hamburgerBox').style.display = none;
});

Alternately, you could bind another click event to the document body or the document itself that would do the same thing.或者,您可以将另一个单击事件绑定到文档正文或文档本身,以执行相同的操作。

That being said, what you really want to be doing is trying to keep a separation of concerns, which in this case means keeping your CSS out of your JS and your JS out of your HTML.话虽如此,您真正想做的是尝试保持关注点分离,在这种情况下,这意味着将 CSS 与 JS 隔离,将 JS 与 HTML 隔离。 Instead of using the event attributes in HTML, add event listeners for the element in your JS file.不要使用 HTML 中的事件属性,而是为 JS 文件中的元素添加事件侦听器。 Instead of changing CSS properties in your JS, change the class that is applied to the element.不要更改 JS 中的 CSS 属性,而是更改应用于元素的类。 In this case, that would look like adding and removing a class called ".is-visible" or something like that to the dropdown menu in your events.在这种情况下,这看起来就像在事件的下拉菜单中添加和删除一个名为“.is-visible”或类似的类。

Check out this thread where it talks about removing classes when the screen is clicked: click anywhere on the screen to remove class查看此线程,其中讨论了单击屏幕时删除类单击屏幕上的任意位置以删除类

Here are some other helpful resources for you regarding hamburger button menus:以下是一些有关汉堡按钮菜单的其他有用资源:

One way to achieve the result, using pure Javascript, is to hide the menu when a user click on the document object and stop event bubbling on click for the hamburger-icon element, like this:使用纯 Javascript 实现结果的一种方法是在用户单击文档对象时隐藏菜单,并在单击汉堡图标元素时停止事件冒泡,如下所示:

// Intercept click on the hamburger icon, stop the event bubbling and show the hamburger box.
document.getElementById( 'hamburger-icon' ).addEventListener( 'click', function( event ) {
    event.stopPropagation();
    document.getElementById( 'hamburgerBox' ).style.display = 'inline';
});

// Intercept the click on the document and hide the hamburger box. Having stopped the bubbling, the click on the hamburger icon will never reach this listener.
document.addEventListener( 'click', function() {
    document.getElementById('hamburgerBox').style.display = 'none';
});

Note I've added the ids to the element to make javascript reach them easily, so in your code you have to put them as well in order for the code to work.请注意,我已将 id 添加到元素中以使 javascript 轻松访问它们,因此在您的代码中,您必须将它们也放入以便代码工作。

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

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