Reputation: 131
Is there a way to trigger a mouseover event only after the mouse was hovering for 1 sec on an element?
$("img").mouseover(function () {
$(this).animate( {opacity:1}, 200);
});
Upvotes: 1
Views: 138
Reputation: 140210
$("img").on("mouseover mouseout", function() {
var tid = 0;
return function(e) {
var elem = $(this);
clearTimeout(tid);
if (e.type == "mouseover") {
tid = setTimeout(function() {
elem.stop(true).animate({
opacity: 1
}, 200);
}, 1000);
}
else {
console.log(elem);
elem.stop(true).animate({
opacity: 0.3
}, 200);
}
};
}());
Upvotes: 3
Reputation: 13421
$("img").mouseover(function () {
$(this).delay(1000).animate( {opacity:1}, 200);
}).mouseleave(function() {
$(this).clearQueue().stop().animate({opacity:0.2},200);
});
Upvotes: 1
Reputation: 2278
you can create a timer function (see [1]) that processes your event 1s later. You can store that function in an array or directly to "window", to let it be cancelable if "mouseout" occurs before the timer function triggers.
[1] http://www.w3schools.com/js/js_timing.asp
Upvotes: 0
Reputation: 20820
You should use setTimeOut function.
setTimeOut(function(){$("img").mouseover(function () {
$(this).animate( {opacity:1}, 200);
});
},1000);
It takes time in milliseconds.
Upvotes: 0
Reputation: 571
You can use the hoverIntent() jQuery plugin found here: http://cherne.net/brian/resources/jquery.hoverIntent.html
Also, make sure you be careful when using these kinds of things as they do not work on mobile browsers or anything using a touch screen.
Upvotes: 2