I have menu items in a navigation, and when they're hovered, I apply a class, and when they're not hovered, remove a class via .toggleClass().
However, if someone clicks on one of these menu item and their mouse remains on top of that menu as the page reloads, when the page reloads, the class is being added when the mouse no longer hovers over that menu item and removed when the menu item is hovered -- exact opposite of what I want.
HTML
<div class="mega-menu">
<div class="mega-menu-2 mega-menu-item">{mega-menu-2:content}</div>
</div>
jQuery
$('.wsite-nav-2, .mega-menu-2').on('hover', function(e) {
$('.nav, .nav .container').css('overflow', 'initial');
e.preventDefault();
$('.mega-menu-2').removeClass('show');
$('.mega-menu-2').toggleClass('show');
});
.removeClass
followed by.toggleClass
is the same asaddClass
.toggleClass
unless you have full control (which you don't). Explicitly useaddClass
/removeClass
. You could check if the mouse is over the item on page load, eg with this: stackoverflow.com/a/42539345/2181514 or something like$(".mega-menu-2').is(":hover")
(see here for code compatible with latest jquery: stackoverflow.com/a/8981521/2181514) .:hover