jQuery 动画“完成”回调的正确语法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18374369/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Correct syntax for jQuery animate "complete" callback
提问by Saturnix
I have to use a callback function when a jQuery animation is complete. I've always did that like this:
当 jQuery 动画完成时,我必须使用回调函数。我一直都是这样做的:
.animate({ properties }, duration, function() { /* callback */ });
However, when asking a question here I've been proposed a solution with a different syntax.
但是,当在这里提出问题时,我提出了一个具有不同语法的解决方案。
$('#redirectNoticeContainer').animate({ properties }, { queue: false, duration: 123 });
where am I supposed to put the callback function there? This is my guess, but it is not working.
我应该把回调函数放在哪里?这是我的猜测,但它不起作用。
$('#redirectNoticeContainer').animate({ properties }, { queue: false, duration: 123 }, function () {
console.log("ok");
setTimeout(function () { window.location.replace("/Account/Login"); }, 1200);
});
The animation works. Callback doesn't. Where should I put it?
动画作品。回调没有。我应该把它放在哪里?
回答by Frédéric Hamidi
That form of animate()
takes a complete
option:
这种形式animate()
需要一个complete
选项:
$("#redirectNoticeContainer").animate({
// properties...
}, {
queue: false,
duration: 123,
complete: function() {
console.log("ok");
setTimeout(function() {
window.location.replace("/Account/Login");
}, 1200);
}
});
回答by Simon_Weaver
You can also use 'promises' since jquery 1.6
自 jquery 1.6 起,您还可以使用 'promises'
$('.play-button').animate({ opacity: 0 }).promise().done(function ()
{
$('.video').addClass('playing');
});
See also How to get jQuery to wait until an effect is finished?