0

So here's my code:

   var timeouts = {};
    $('#nav ul > li').mouseenter(function() {
        clearTimeout(timeouts['menu']);
        $(this).find('div.dropdown').stop(true).slideDown(200);
    });
    $('#nav ul > li').mouseleave(function() {
        timeouts['menu'] = setTimeout(function() {
            $(this).find('div.dropdown').stop(true).slideUp(200);
        }, 1000)
    });

This doesn't seem to be working. Any idea? Is there any other way to achieve my goal? The code now works when I hover my mouse over but the div will not slide up when my mouse leaves.

2 Answers2

0

Inside of your anonymous function, this is probably not defined as a HTML element.

Put this in the proper scope:

var timeouts = {'menu': false};

$('#nav ul > li').hover(function() {
    if (timeouts['menu'] !== false) {
      clearTimeout(timeouts['menu']);
    }

    $(this).find('div.dropdown').stop(true).slideDown(200);
}, function() {
    var $this = $(this);

    timeouts['menu'] = setTimeout(function() {
        $this.find('div.dropdown').stop(true).slideUp(200);
    }, 1000)
});

Also, you're probably going to get an error if you try to call timeouts['menu'] without it being defined first. I'd set it to a default value and then check if it exists before using it.

Blender
  • 289,723
  • 53
  • 439
  • 496
0

You could try the hoverIntent plugin or have a look at some of the solutions here: jQuery if mouse is over for 2 sec. begin animation or don't animate at all

Community
  • 1
  • 1
Moin Zaman
  • 25,281
  • 6
  • 70
  • 74