jQuery 使用淡入/淡出添加/删除类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15731879/
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 add/remove Class with fadeIn/Out
提问by Swim89
I would to apply a fadeIn effect to a addClass function..and fadeOut to removeClass...
我想将淡入效果应用于 addClass 函数......并将淡出效果应用于 removeClass ......
Can you help me?
你能帮助我吗?
This is my code
这是我的代码
$('#loader'+idTurno).addClass('loader');
...
...
$('#loader'+idTurno).removeClass('loader');
回答by What have you tried
Fade In:
淡入:
$("#loader").fadeIn("slow", function() {
$(this).addClass("loader");
});
Fade Out:
淡出:
$("#loader").fadeOut("slow", function() {
$(this).removeClass("loader");
});
As another user said, you may want to look into using toggleClass
.
正如另一位用户所说,您可能想考虑使用toggleClass
.
回答by enguerranws
Another way to achieve that, using your original jQuery code, the CSS way :
另一种实现方式,使用您的原始 jQuery 代码,CSS 方式:
#loader {
transition: opacity 500 ease-in-out;
}
Smoother animation, easier to maintain.
动画更流畅,更易于维护。
回答by Hil
#loader {
transition: all 0.9s ease-out 0s;
}
回答by Sulung Nugroho
Make it simple :
让它简单:
$('#loader'+idTurno).addClass('loader').fadeIn(1000);
$('#loader'+idTurno).removeClass('loader').fadeIn(1000);
回答by imfromthepast
You should add a duration to the remove/addClass method:
您应该向 remove/addClass 方法添加持续时间:
$('#loader'+idTurno).addClass('loader',500);
$('#loader'+idTurno).removeClass('loader',500);