检查元素是否正在淡出(Javascript/Jquery)
有没有办法检查元素当前是否正在淡出 类似这样的代码(伪代码): isFadingOut()就是我在这里需要的。检查元素是否正在淡出(Javascript/Jquery),javascript,jquery,html,web-applications,show-hide,Javascript,Jquery,Html,Web Applications,Show Hide,有没有办法检查元素当前是否正在淡出 类似这样的代码(伪代码): isFadingOut()就是我在这里需要的。 它以某种方式存在吗?当您实际淡出时,您可以添加一个触发器,如下所示: $(element).fadeOut("normal", function () { // your other code $(this).trigger('isFadingOut'); }); 然后在别的地方听这个事件 $(element).on('myFadeOutEvent',function() { /
它以某种方式存在吗?当您实际淡出时,您可以添加一个触发器,如下所示:
$(element).fadeOut("normal", function () {
// your other code
$(this).trigger('isFadingOut');
});
然后在别的地方听这个事件
$(element).on('myFadeOutEvent',function() {
// do something
});
用您正在使用的任何东西替换该元素。注意:如果您正在使用JQuery,这将起作用。如果您只想检查某些东西是否已经褪色,则只需检查css
不透明度值是否小于1.0:
JSFiddle:
这是jQuery扩展所需的代码:
$.fn.isFading = function(){
return this.css('opacity') < 1;
};
或者使用jQuery动画提供的动画promise
:
element.fadeOut().promise().done(function(){
// do something on completion of all animations for this element
});
更新:
注意:如果元素已处于最终状态,则某些动画事件不会激发done
。在promise
上使用always()
:
element.fadeOut().promise().always(function(){
// do something on completion of all animations for this element
});
请注意,您无法在Javascript中的循环中检查异步完成情况,因为淡入淡出会在多个浏览器周期中发生。
element.fadeOut().promise().done(function(){
// do something on completion of all animations for this element
});
element.fadeOut().promise().always(function(){
// do something on completion of all animations for this element
});