toggleClass 上的 jquery 函数完成了吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10321393/
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
jquery function on toggleClass complete?
提问by panthro
How can I do a function once a toggleClass has completed? I've tried the following but with no luck:
一旦 toggleClass 完成,我该如何做一个功能?我尝试了以下但没有运气:
$("#loader").toggleClass('fadeOut', function () {
? ? alert('a');
});
回答by Kevin B
jQuery has a promise method that returns a promise that resolves after all running animations on selected elements are complete. At that point, you can bind to it's done method.
jQuery 有一个promise 方法,它返回一个promise,该promise 在选定元素上的所有运行动画完成后解析。此时,您可以绑定到它的 done 方法。
$("#loader").toggleClass('fadeOut',600).promise().done(function(){
console.log('a');
});
http://jsfiddle.net/skram/4x76J/
http://jsfiddle.net/skram/4x76J/
Note:Animations using toggleClass
require jQuery UI.
注意:使用动画toggleClass
需要 jQuery UI。
回答by Selvakumar Arumugam
toggleClass
is executed immediately and so you don't need a callback for toggleClass
, just place the alert('a')
in the next line which will alert after toggleClass
is executed.
toggleClass
立即执行,因此您不需要回调toggleClass
,只需将alert('a')
放在下一行,toggleClass
执行后将发出警报。
Edit:Looks like you want jQuery UI Effects - toggleClass
which unfortunately doesn't have a callback function.
编辑:看起来你想要 jQuery UI 效果——toggleClass
不幸的是它没有回调函数。
Probably you should write your own toggle function. See below for fadeIn/fadeOut toggle,
可能您应该编写自己的切换功能。请参阅下面的淡入/淡出切换,
var $loader = $("#loader");
if ($loader.is(':visible')) {
$loader.fadeOut(100, function () {
alert('fade out complete');
});
} else {
$loader.fadeIn(100, function () {
alert('fadeIn complete');
});
}