[英]Using setTimeout() to delay an event
This is a working function that I've been using to add/remove a class to a separate element immediately when one element has been hovered on:这是一个工作功能,当一个元素悬停在一个元素上时,我一直使用它立即将一个类添加/删除到一个单独的元素:
$(document).ready(function() {
$('.mm-parent:not(.mm-active)').hover(function(){
$('.mm-active').addClass('activefix');
},
function(){
$('.mm-active').removeClass('activefix');
});
});
However, I'm trying to:但是,我正在尝试:
I don't know if I'm on the right track, but here is what I have:我不知道我是否在正确的轨道上,但这是我所拥有的:
$(document).ready.setTimeout(function() {
$('.mm-parent:not(.mm-active)').hover(function(){
$('.mm-active').addClass('activefix');
},
function(){
$('.mm-active').removeClass('activefix');
}, 1000);
});
Try尝试
$(document).ready(function () {
var timer;
$('.mm-parent:not(.mm-active)').hover(function () {
clearTimeout(timer);
timer = setTimeout(function () {
$('.mm-active').addClass('activefix');
}, 1000)
}, function () {
clearTimeout(timer);
timer = setTimeout(function () {
$('.mm-active').removeClass('activefix');
}, 1000)
});
});
Try this尝试这个
$(document).ready(function() {
$('.mm-parent:not(.mm-active)').hover(function(){
setTimeout(function(){
$('.mm-active').addClass('activefix');
setTimeout(function(){$('.mm-active').removeClass('activefix');},1000);
},1000);
});
});
Basically you have to run a function 1 sec after hover which internally runs one more function after 1 sec.基本上,您必须在悬停 1 秒后运行一个函数,该函数在 1 秒后在内部再运行一个函数。
JsFiddle http://jsfiddle.net/L8B73/ JsFiddle http://jsfiddle.net/L8B73/
Use CSS transition,that might serve your need.使用 CSS 过渡,这可能会满足您的需求。
-webkit-transition: all 1s ease-out; /* Chrome 1-25, Safari 3.2+ */
-moz-transition: all 1s ease-out; /* Firefox 4-15 */
-o-transition: all 1s ease-out; /* Opera 10.50–12.00 */
transition: all 1s ease-out;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.