Selector animated

The :animated selector selects elements that are currently being animated. If you are using a custom jQuery build without the effects module, using :animated will throw an error. Since :animated is not part of the CSS spec, for better performance in modern browsers it is better to first filter elements using a pure CSS selector, and then apply .filter(':animated').

Syntax

This is how we select the elements involved in the animation:

javascript$(':animated');

Example

Let's make one of the squares to start an animation using the animateIt function. Each time the button with #change is clicked, the color of the animated square will change from green to red and back - by removing and adding the colored class using the toggleClass method:

html<button id="change">change</button>

<div></div>
<div id="test"></div>
cssdiv {
  background: green;
  border: 1px solid #AAA;
  width: 80px;
  height: 80px;
  margin: 0 5px;
  float: left;
}
div.colored {
  background: red;
}
javascript$('#change').click(function() {
  $('div:animated').toggleClass('colored');
});

function animateIt() {
  $('#test').slideToggle('slow', animateIt);
}

animateIt();

See also

  • method slideToggle,
    which shows hidden elements and hides shown ones
  • method toggleClass,
    which adds or removes a CSS class
  • method find,
    which searches for elements within those already found