1

我有一个元素,我想在鼠标悬停事件上做点什么!我试过这个:

$("#myElement").hover(
function () { // in 
    ...
},
function () { // out
    ...
});

但它不应该立即运行。我想在几秒钟后触发悬停事件。我该如何实施?

4

1 回答 1

5

你会等setTimeout()

在发布这个问题之前,你真的应该多搜索一点。

(function() {

    var time = 1000,
        timer;

    function handlerIn() {
        clearTimeout(timer);
    }
    function handlerOut() {
        timer = setTimeout(function() {
            $('.target').fadeOut(3000);
        }, time);
    }

    $("#myElement").hover(handlerIn, handlerOut);

}());

这里发生的事情很简单,在悬停时启动设置为的计时器1000,但是当你悬停时它会取消clearTimeout()你刚刚设置的计时器

于 2013-07-10T07:26:48.480 回答