Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/399.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么可以链接jquery效果,而不是使用回调_Javascript_Jquery_Animation_Effects - Fatal编程技术网

Javascript 为什么可以链接jquery效果,而不是使用回调

Javascript 为什么可以链接jquery效果,而不是使用回调,javascript,jquery,animation,effects,Javascript,Jquery,Animation,Effects,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 w

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();
但是,您可以链接两种效果,而无需使用回调:

// 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();
});

回调对于异步操作是有意义的。因此,回调是指如果您希望在淡入结束后发生某些事情。在您的情况下,后续链接的
.addClass()
不会等待淡入完成。它马上就会发生。

根据我的理解,当您执行像fadeIn/fadeOut这样的操作时,您所做的是创建一个请求,该请求被放置在一个内部动画队列中,该队列以FIFO方式执行操作。其他人可以对此进行更详细的说明,但由于它是这样工作的,因此不需要使用回调。

因为,简单地说,对
.fadeIn
的调用不会等待。它所做的只是将效果添加到队列中,然后继续使用您拥有的任何其他代码。jQuery文档的意思是,如果您想在效果完成后(他们只是作为示例使用)向元素添加一个类,那么必须使用回调函数,回调函数在效果结束时调用


此外,您完全可以通过使用回调来链接两种效果,但这是不必要的输入量(而且开发人员通常希望尽可能地懒惰)。它在不回调的情况下将两个效果链接在一起的原因是调用
.fadeIn
.fadeOut
会为该DOM元素向jQuery的效果队列添加适当的效果。效果从队列中一次播放一个,按顺序播放,因此无论您以多快的速度将它们添加到队列中,它们都将以正确的速度和时间播放。

再添加一点:jQuery通过在调用中返回对象来实现链。这就是为什么你可以链接调用,这是同步行为。这是真的,也是这种(公认的方便)方法不受欢迎的原因之一。这是一个相当严重的副作用,很难理解/管理。回调方法很好,但在语法方面很笨拙,这就是人们喜欢jQuery链接的原因。如果这是在今天实现的,它肯定是基于承诺的,这将使序列简洁而不添加特殊队列(例如,
(wait$thing.fadeOut()).fadeIn()
)这里有一条重要线索:尝试链接一个需要时间的效果,看看会发生什么。基本上,它可以归结为您希望在链中应用下一个内容的时间。应该注意的是,如果没有将持续时间传递给jQuery动画方法,则其行为与将持续时间传递给该方法的行为不同。