I am trying to toggle a class on a navbar toggle but when you move your mouse the the dropdown menu it quickly removes the class and hides the menu. I think what it needs is a delay on just the removal of the class but the showing of the class can still be instant. Here is a screenshot of the hover issue https://i.imgur.com/o8ccCn9.gifv
Is there a manual way to write a toggle?
Codepen: https://codepen.io/JacobLett/pen/jaaQYG?editors=0110
This is the script so far
$(document).ready(function() {
// executes when HTML-Document is loaded and DOM is ready
// when you hover a toggle show its dropdown menu
$(".navbar .dropdown-toggle").hover(function () {
$(this).parent().toggleClass("show");
$(this).parent().find(".dropdown-menu").toggleClass("show");
});
// hide the menu when the mouse leaves the dropdown
$( ".navbar .dropdown-menu" ).mouseleave(function() {
$(this).removeClass("show");
});
// document ready
});
Update I guess .hover doesn't take into consideration the child elements. If it did my code would work. So I tried mouseenter and mouseleave but since the dropdown is position absolute it doesn't work with this either.
Ok, after trying a few javascript options I remembered the good old suckerfish menu which had you move the dropdown menu up a few pixels so the hover state would not break as you hovered over the gap between the anchor trigger and the menu itself.
So I kept my script the same but fired it only on desktop widths and then adjusted my CSS to remove the hover gap.
Final Solution: https://codepen.io/JacobLett/pen/jaaQYG
HTML
CSS
JS
You could use the native JavaScript
setTimeout()
method...