spectre icon indicating copy to clipboard operation
spectre copied to clipboard

How to close a dropdown menu

Open wgehner opened this issue 6 years ago • 6 comments

I have a dropdown menu item that does a window.open in new tab (which works). But in the old tab the menu remains visible. I tried jQuery $('.dropdown-toggle').blur() but that doesn't remove the menu.

wgehner avatar Oct 22 '18 22:10 wgehner

You can replace it by: $('.menu').css('display','none')

It will remain visible because the .menu is still hovered so it didn't hide according to the code. Thus, the best solution is hide manually using JQuery.

Tex-Tang avatar Nov 19 '18 14:11 Tex-Tang

Is there a way to get the drop down closing on mobile that’s lighter weight than JQuery? My site is CSS only so I wanted to avoid including JQuery just for this.

kiwioverseas avatar Jan 29 '19 19:01 kiwioverseas

@offradar you can assign an ID to your menu and use something like:

document.getElementById("myMenuID").style.display = "none";

More info at https://www.w3schools.com/js/js_htmldom_css.asp

laurentpayot avatar Jan 30 '19 11:01 laurentpayot

Same problem here. Suggested fix is not working. When is applied then we can't show dropdown menu after click again.

NeoRazorX avatar Mar 11 '19 15:03 NeoRazorX

I started using dropdowns and encountered the same issue. Here is a CSS-only fix that worked for me (tested on Chromium, Firefox and Webkit):

.dropdown .menu:active {
  /* animation needed for click events to work */
  animation: bye 0;
}
@keyframes bye {
  to { display: none; }
}

laurentpayot avatar Feb 11 '21 08:02 laurentpayot

Update: my above fix is actually working for non-touch devices only. To make it work also for touch devices just add the following below it:

@media(hover: none) {
  .dropdown {
    .menu:hover {
      display: none;
    }
    .menu:active {
      display: block;
    }
  }
}

laurentpayot avatar Feb 17 '21 13:02 laurentpayot