jQuery jQuery延迟不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4544126/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery delay not working
提问by thenengah
$('.transparent').removeClass('transparent').delay(2000).addClass('not_transparent').delay(4000)
I have a div that is semi transparent and then want to switch it to not transparent. But the jQuery .delay(); method doesn't seem to work here. I've tried .fadeIn(); instead and that works with a delay but it doesn't work the changing classes.
我有一个半透明的 div,然后想将其切换为不透明。但是 jQuery .delay(); 方法在这里似乎不起作用。我试过 .fadeIn(); 相反,这会延迟,但它不适用于不断变化的类。
回答by user113716
.delay()
is used for items that are part of a queue
, like animations. A simple addClass
is not queued.
.delay()
用于属于 a 的项目queue
,如动画。一个简单addClass
的不排队。
You could use setTimeout
.
你可以使用setTimeout
.
var trans = $('.transparent').removeClass('transparent');
setTimeout(function() {
trans.addClass('not_transparent');
}, 2000);
As an alternative, you could add the non-queued item to the queue using .queue()
, though I think a setTimeout
would be better.
作为替代方案,您可以使用 将非排队项目添加到队列中.queue()
,但我认为 asetTimeout
会更好。
$('.transparent').removeClass('transparent').delay(2000).queue(function(nxt) {
$(this).addClass('not_transparent');
nxt();
});
回答by Lewis
I know this is an old question, but there's still a lot of traffic coming here from google so I'll add my two cents;
我知道这是一个老问题,但仍然有很多来自谷歌的流量,所以我会加上我的两分钱;
You could use something like -
你可以使用类似的东西 -
$('.transparent').fadeIn().delay(500).queue(function(){
$('.transparent').addClass('yourclass');
});
You can pass a function to the queue in order to execute them after the delay. Have been using this myself for very similar examples.
您可以将函数传递给队列,以便在延迟后执行它们。我自己一直在使用这个非常相似的例子。
回答by yungtechboy1
.delay()
does not work with the .addClass()
tag outside of a function so just use:
.delay()
不适.addClass()
用于函数外部的标签,因此只需使用:
delay();
function delay(){
$('.pgtitle').delay(5000).fadeIn(0).addClass('animated bounceInDown');
}