104

I'm trying to move some elements on the page, and during the time the animation occurs, I want to have "overflow:hidden" applied to an elemnt, and "overflow" back to "auto" once the animation is completed.

I know jQuery has an utility function that determines whether some element is being animated but I can't find it anywhere in the docs

5 Answers5

203
if( $(elem).is(':animated') ) {...}

More info: https://api.jquery.com/animated-selector/


Or:

$(elem)
    .css('overflow' ,'hidden')
    .animate({/*options*/}, function(){
        // Callback function
        $(this).css('overflow', 'auto');
    };
Studocwho
  • 2,404
  • 3
  • 23
  • 29
James
  • 109,676
  • 31
  • 162
  • 175
  • yes that was it, thanks. how could I miss it? damn, I'm getting old –  Apr 07 '09 at 13:19
  • FYI, if you don't want to risk overwriting your CSS styles by programming "overflow: auto" in the callback, simply use `.css('overflow', '')`. Passing an empty string generally removes that property from the element's style altogether. Not sure if this is documented behavior, but it's a very useful trick. – Ward D.S. Apr 29 '16 at 08:38
  • 2
    I don't think it supports CSS animations. – Gqqnbig Jan 21 '17 at 02:28
5

Alternatively, to test if something is not animated, you can simply add a "!":

if (!$(element).is(':animated')) {...}
Bill
  • 3,478
  • 23
  • 42
Tim
  • 123
  • 2
  • 5
  • This is very not true... The opposite of jquery 'is' is not 'not'. 'not' removes filtered elements from the jquery object. If you want to check for not animated object you do `if (!$(element).is(':animated')) {...}` – amosmos Jul 22 '15 at 12:16
  • 1
    @amosmos The answer was edited and community approved.. I have rolled it back to where it was correct. – Bill Aug 24 '15 at 11:08
  • @amosmos stop trolling – Eric Cicero May 11 '17 at 16:25
2

if you are using css animation and assign the animation by using specific class name, then you can check it like this:

if($("#elem").hasClass("your_animation_class_name")) {}

But make sure that you are removing the class namewhich is handling the animation, after the animation is finished!

This code can be used to remove the class name after the animation is finished:

$("#elem").on('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend',
function(){ 
        $(this).removeClass("your_animation_class_name");
});
Ari
  • 4,643
  • 5
  • 36
  • 52
0

If you want to apply css to animated elements, you can use the :animated pseudo selector and do it like this,

$("selector").css('overflow','hidden');
$("selector:animated").css('overflow','auto');

source : https://learn.jquery.com/using-jquery-core/selecting-elements/

Lucky
  • 16,787
  • 19
  • 117
  • 151
0
$('selector').click(function() {
  if ($(':animated').length) {
    return false;
  }

  $("html, body").scrollTop(0);
});
Erazihel
  • 7,295
  • 6
  • 30
  • 53
  • While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. Please read this [how-to-answer](http://stackoverflow.com/help/how-to-answer) for providing quality answer. – thewaywewere Jun 26 '17 at 07:19