1

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);

});
mshell_lauren
  • 5,171
  • 4
  • 28
  • 36
Mali
  • 131
  • 8

5 Answers5

3

http://jsfiddle.net/tqa2J/1/

$("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);
        }

    };
}());​
Esailija
  • 138,174
  • 23
  • 272
  • 326
2

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.

Marc
  • 561
  • 5
  • 17
1
$("img").mouseover(function () {
    $(this).delay(1000).animate( {opacity:1}, 200);
}).mouseleave(function() {
    $(this).clearQueue().stop().animate({opacity:0.2},200);
});​

DEMO

Okan Kocyigit
  • 13,203
  • 18
  • 70
  • 129
0

You should use setTimeOut function.

setTimeOut(function(){$("img").mouseover(function () {

        $(this).animate( {opacity:1}, 200);

});
},1000);

It takes time in milliseconds.

Nishu Tayal
  • 20,106
  • 8
  • 49
  • 101
0

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

Arcadien
  • 2,258
  • 16
  • 26