【发布时间】:2017-06-06 23:14:20
【问题描述】:
在 jquery 文档中声明,如果要在对选择应用效果后添加一个类,则必须使用回调:
// Fade in all hidden paragraphs; then add a style class to them (not quite right)
$( "p.hidden" ).fadeIn( 750 ).addClass( "lookAtMe" );
// Fade in all hidden paragraphs; then add a style class to them (correct with animation callback)
$( "p.hidden" ).fadeIn( 750, function() {
// this = DOM element which has just finished being animated
$( this ).addClass( "lookAtMe" );
});
但是,您可以链接两个效果而不需要使用回调:
$(".something").fadeOut().fadeIn();
这是为什么呢?你不应该也使用这样的回调吗:
$( ".something" ).fadeOut( 750, function() {
$( this ).fadeIn();
});
【问题讨论】:
-
尝试链接一个需要时间的效果,看看会发生什么。基本上它归结为你想要应用链中的下一个东西的时间。立即,或在前一件事完成后。
-
需要注意的是,当没有将持续时间传递给jQuery动画方法时,其行为与将持续时间传递给方法的行为不同。
标签: javascript jquery animation effects