我有一个元素,我想在鼠标悬停事件上做点什么!我试过这个:
$("#myElement").hover(
function () { // in
...
},
function () { // out
...
});
Run Code Online (Sandbox Code Playgroud)
但它不应该立即运行.几秒钟后我想要火灾悬停事件.我怎么能实现这个?
你会等 setTimeout()
在发布此问题之前,你应该真的要多搜索一下.
(function() {
var time = 1000,
timer;
function handlerIn() {
clearTimeout(timer);
}
function handlerOut() {
timer = setTimeout(function() {
$('.target').fadeOut(3000);
}, time);
}
$("#myElement").hover(handlerIn, handlerOut);
}());
Run Code Online (Sandbox Code Playgroud)
这里发生的事情很简单,在hoverin启动时设置的计时器1000,但当你徘徊时取消clearTimeout()你刚刚设置的计时器