Reputation: 72530
I'm working on a jQuery drop-down menu that fades in when you hover on the top-level items. I want to set it so that when you move the mouse away the menu doesn't disappear instantly. I have this code:
$(document).ready(function(){
$('ul#menu > li').hover(
// mouseover
function(){
$(this).find('>ul').fadeIn('fast');
},
// mouseout
function(){
setTimeout( function(){
alert('fadeout');
$(this).find('>ul').fadeOut('fast')
}, 1000 );
}
);
});
After a second the alert happens, but the menu isn't faded out.
Upvotes: 2
Views: 9183
Reputation: 342635
Have a look at hoverIntent. It'll give you greater control of the behaviour of the mouseover
/mouseout
events by configuration:
var config = {
sensitivity: 3, // number = sensitivity threshold (must be 1 or higher)
interval: 200, // number = milliseconds for onMouseOver polling interval
timeout: 500, // number = milliseconds delay before onMouseOut
};
$(document).ready(function(){
$('ul#menu > li').hoverIntent(
// mouseover
function(){
$(this).find('>ul').fadeIn('fast');
},
// mouseout
function(){
$(this).find('>ul').fadeOut('fast');
}
);
});
Upvotes: 3
Reputation: 15268
window.setTimeout(), so this refers to the window object.
// mouseout
function(){
var el=this;
setTimeout( function(){
alert('fadeout');
$(el).find('>ul').fadeOut('fast')
}, 1000 );
}
Upvotes: 3