tec*_*rek 7 jquery callback chaining
对于jQuery,以下两个片段的结果有什么不同.什么?我是否更正了第一个动画完成时回调和链中的第二个项目都被执行了?
$(selector).animate({ opacity: 1 }).animate({ opacity: 0.5 });
Run Code Online (Sandbox Code Playgroud)
VS
$(selector).animate({ opacity: 1}, function()
{
$(this).animate({ opacity: 0.5 });
});
Run Code Online (Sandbox Code Playgroud)
在哪种类型的情况下,我想要使用哪一种?如果我需要做更复杂的事情或切换到不同的选择器,我会只使用后者吗?
提前致谢.
小智 10
它们实际上是相同的,所以你可能只是使用第一个.
回调(第二个版本)用于运行任何未自动排队的任意代码.
这包括其他jQuery方法.css(),例如,如果不在回调中,将在动画完成之前很久就运行.
// .animate() is automatically queued
$(selector).animate({ opacity: 1 }).animate({ opacity: 0.5 });
Run Code Online (Sandbox Code Playgroud)
// .css() is not automatically queued, so you'd need a callback
$(selector).animate({ opacity: 1 }, function() {
$(this).css({ opacity: 0.5 });
});
Run Code Online (Sandbox Code Playgroud)
没有回调......
// Animation starts ----v
$(selector).animate({ opacity: 1 }).css({ opacity: 0.5 });
// ...but .css() runs immediately-------------^
// ...because it isn't automatically added to the queue.
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3375 次 |
| 最近记录: |