Overview: I am creating a menu that shows a div 'showme' when you hover over a list item with class 'showhim'. I want a delay when switching between a list item so I included a setTimeout function.
Problem: The setTimeout function is not being triggered on the first mouseenter event. If I remove the setTimeout function the mouseenter event works fine. mouseenter
Tried: I've tried mouseover and mouseout to see if it would make a difference. If I put my mouse over the span (the menu item name) the setTimeout function would trigger.
Also, when I put a border on the top most parent div and hover over the border before entering the 'showhim', the setTimeout function is triggered and the menu works fine. I have no clue why the border would make it work..
// Fake these for the snippet
var isHomepage = false;
function isMobile() {
return false;
}
// End of fake
$('.showhim').mouseenter(function() {
if (!isMobile()) {
var $this = $(this);
timer = setTimeout(function() {
$('.mastermenuitem').removeClass('menu-active');
$('.mainmenuitem').removeClass('nav-active');
$('.showme').css('display', 'none');
// Expand Shop Menu Container
if (isHomepage && !isMobile()) {
var corecenter_width = $('#corecenter').innerWidth();
$('#shop-menu-container').css('width', corecenter_width - (border_width * 2));
}
// Add active class to clicked menu item
$this.find('li').addClass('menu-active');
$this.find('.showme').css('display', 'block');
$this.find('.mainmenuitem').addClass('nav-active');
}, 150);
}
}).mouseleave(function() {
clearTimeout(timer);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<!-- setTimeout triggered when hovering over shop-menu-container border -->
<div id="shop-menu-container">
<div class="menu-wrapper">
<div class="showmemaster">
<div class="showhim-wrapper">
<!-- Trigger Element-->
<div class="showhim">
<!-- mouseover works the first time when hovering over the span -->
<ul><li class="mainmenuitem"><span>Menu Item 1</span></li></ul>
<div class="showme" style="display:none;">
Content 1
</div>
</div>
<div class="showhim">
<ul><li class="mainmenuitem"><span>Menu Item 2</span></li></ul>
<div class="showme" style="display:none;">
Content 2
</div>
</div>
</div>
</div>
li
cannot be the direct child of anything buttemplate
,ul
, andol
. – T.J. CrowderisHomepage
andisMobile
and putting something to show inside the.showme
). Works in the snippet, the problem lies elsewhere (perhaps in that variable and function?). This is the advantage of creating a true MCVE. – T.J. Crowder