JQuery animation detects if animation?

Is this a way to detect if an element is animating or detects if the values โ€‹โ€‹of an element change?

Because I need to call a function if the animation element. Not onComplete animation.

+8
jquery
source share
3 answers

When the selected item is displayed, the following is returned:

var isAnimating = $("#someid").is(':animated'); 

More details:

http://api.jquery.com/animated-selector/
and / or
http://api.jquery.com/animate/
step: The function that will be called after each step of the animation.

+21
source share

An easy way would be to add a global boolean value that will be set to true as soon as the animation starts. Then you add a callback function to the animation, which sets it to false as it completes.

 var running = false; $('#start').click(function(){ running = true; $('#target').animate({opacity: 0.5},'slow',function(){ running = false; }); }); 

Edit: Oh, I think there is a selector there.

+1
source share

Because I need to call a function if the animation element. Not onComplete animation.

Since $(element).on(":animated", function(){ ... }); doesn't seem to work, the only way I can see is to very simply execute the function that you want to call when the animation is executed:

 $(selector).animate( . . . ); functionToCall(); //called with animate above 

Another solution is to create your own library that handles the start of the animation, and also processes calls to the necessary functions after it starts the animation in the simple way described above.

So just psuedocode:

 var animation = new MyAnimationLibrary(function() { $(selector).animate( ... ) }); animation.onStart.push(function() { alert('animation starts'); }); //then later in the code: animation.start(); 

Then just make the above function call in the animation.onStart array and start the animation.

0
source share

All Articles