下面是链接与回调函数的 JQuery 代码的代码片段:-
$(selector).animate({ opacity: 1 }).animate({ opacity: 0.5 });
对比
$(selector).animate({ opacity: 1}, function()
{
$(this).animate({ opacity: 0.5 });
});
在什么类型的情况下,人们会选择其中一种而不是另一种?
最佳答案
它们实际上是相同的,因此您可能只使用第一个。
回调(第二个版本)用于运行任何不自动排队的任意代码。
这包括其他 jQuery 方法,例如 .css()
,如果不在回调中,它将在动画完成之前运行很长时间。
// .animate() is automatically queued
$(selector).animate({ opacity: 1 }).animate({ opacity: 0.5 });
<小时/>
// .css() is not automatically queued, so you'd need a callback
$(selector).animate({ opacity: 1 }, function() {
$(this).css({ opacity: 0.5 });
});
<小时/>
没有回调...
// Animation starts ----v
$(selector).animate({ opacity: 1 }).css({ opacity: 0.5 });
// ...but .css() runs immediately-------------^
// ...because it isn't automatically added to the queue.
关于JQuery:链接函数与回调函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9314235/