请参阅马特对this question的回答。他说,.delay()
让大多数人感到困惑,他给出了以下示例:
$('#foo').hide().delay(2000).slideDown().text('Hello!').delay(2000).hide();
有关小提琴的信息,请参见here。有人能解释一下这行代码的行为吗?
发布于 2011-09-14 04:07:37
只有jQuery代码的某些部分可以用.delay()
推送-- hide()
和text()
是,而不是。
所以基本上代码所做的是(并不是真的这样做,这只是一个草图):
setTimeout(function(){
$('#foo').slideDown();
}, 2000);
$('#foo').hide().text('Hello!').hide();
这就是为什么slideDown()
似乎最后才发生的原因。
发布于 2011-09-14 05:31:07
它行为不端的原因已经解释过了。要获得所需的行为,请使用queue
$('#foo')
.hide()
.delay(2000)
.slideDown()
.queue(function() {
$(this).text('Hello!').dequeue();
})
.delay(2000)
.queue(function() {
$(this).hide().dequeue();
});
发布于 2011-09-14 04:14:55
delay
会在动画队列中放置延迟(无效果动画),因此它仅影响其他动画。在delay
之后链接hide
不会延迟hide
,它仍然会立即发生。
如果您将这些方法划分为立即影响元素的方法和启动动画的方法,则会得到执行相同操作的代码:
$('#foo').hide().text('Hello!').hide(); // set text and hide twice
$('#foo').delay(2000).slideDown().delay(2000); // wait, slide, wait and do nothing
https://stackoverflow.com/questions/7407764
复制相似问题