Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if an element is fading out (Javascript/Jquery)

is there a way to check if an element is currently fading out?

Something like this (Pseudo Code):

while(element.isFadingOut()){
//do something
}

The isFadingOut() is what I need here. Does it exist somehow?

like image 432
user3813280 Avatar asked Nov 15 '25 22:11

user3813280


1 Answers

If you just want to check that something is already fading, you just need to check the css opacity value for anything less than 1.0:

JSFiddle: http://jsfiddle.net/TrueBlueAussie/vkcpknLh/

This is the required code as a jQuery extension:

$.fn.isFading = function(){
    return this.css('opacity') < 1;
};

Note: the fade is an asynchronous operation, so checking in a loop will never work. You would need to poll it via a timer etc (as per my JSFiddle).

The alternative is to use the completion event of fadeOut:

 element.fadeOut(function() {
       // do something on completion
 });

or use the animation promise provided for jQuery animation:

 element.fadeOut().promise().done(function(){
       // do something on completion of all animations for this element
 });

Update:

Note: Some animation events do not fire done if the element was already in the final state. Use always() on the promise instead:

 element.fadeOut().promise().always(function(){
       // do something on completion of all animations for this element
 });
like image 111
Gone Coding Avatar answered Nov 18 '25 14:11

Gone Coding



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!