Reputation: 381
I'm having trouble attaching a delay to my mouseOut function. Here's the jquery code:
$(document).ready(function() {
$('.nav').mouseover(function() {
$('.nav').css({ left: '160px' });
});
$('.nav').mouseout(function() {
$('.nav').delay(600).css({ left: '0' });
});
});
I figured that adding the delay section in would do it... but it still behaves as though it wasn't there at all.
Suggestions?
EDIT
The suggested setTimeout function worked - but created a new problem. This effect now triggers even when still over the selected '.nav' div.
Here's a jsfiddle of the problem: http://jsfiddle.net/TRL4w/
Upvotes: 2
Views: 1284
Reputation: 28837
Try using .mouseover()
and .stopPropagation();
Example:
$(document).ready(function () {
$('header').hover(function (e) {
e.stopPropagation();
console.log('in');
$('.nav').css({
left: '160px'
});
},function () {
console.log('out');
setTimeout(function () {
$('.nav').css({
left: '0px'
});
}, 2000)
});
});
Upvotes: 1
Reputation: 207861
The delay() function only works with animations. Try:
$('.nav').mouseout(function() {
setTimeout(function(){$('.nav').css({left: '0'});}, 600)
});
From the .delay() docs:
The .delay() method is best for delaying between queued jQuery effects. Because it is limited—it doesn't, for example, offer a way to cancel the delay—.delay() is not a replacement for JavaScript's native setTimeout function, which may be more appropriate for certain use cases.
Upvotes: 6