Skip to content
For the complete documentation index, see llms.txt. This page is also available as Markdown.

Animate

The information in this section is relevant for jQuery version 1.8 and above. If you're interested in extension capabilities for older versions, read my article "Writing Animation Plugins".

First, a teaser — the animate() method manipulates the jQuery.Animation object, which provides the following extension points:

  • jQuery.Tween.propHooks

  • jQuery.Animation.preFilter

  • jQuery.Animation.tweener

I'll start with jQuery.Tween.propHooks, since there are already plugins whose code we can peek at :) For maximum clarity, I'll take a fairly trivial task — let's smoothly change the font color for a given set of elements:

$('p').animate({color:'#ff0000'});

The code above won't produce any effect, because out of the box the library doesn't animate the color property. But we can fix that — we just need to level up jQuery.Tween.propHooks:

$.Tween.propHooks.color = {
    get: function(tween) {
        return tween.elem.style.color;
    },
    set: function(tween) {
        tween.easing;  // current easing
        tween.elem;    // target element
        tween.options; // animation settings
        tween.pos;     // current progress
        tween.prop;    // property being changed
        tween.start;   // starting values
        tween.now;     // current value
        tween.end;     // desired final values
        tween.unit;    // units of measurement
    }
};

This code doesn't work yet — these are our building blocks, and we'll use them to construct our animation. Before we start, it's worth peeking inside each of these properties:

There will be a lot of data in the console, because the method above gets called N times depending on the animation duration, with "tween.pos" gradually increasing its value from 0 to 1. By default, this increase is linear — if you need something different, check out the easing plugin or read on to the end of this section (I already mentioned this in the Animation chapter).

Even at this point we can already modify the selected element (by manipulating tween.elem), but there's a more convenient way — you can set the "run" property of the tween object:

The resulting code will work as follows:

  1. the set() function will be called once

  2. the run() function will be called N times, with progress behaving just like tween.pos

Now, getting back to the original task of changing color, we can whip up the following code:

You'll find the code for parseColor() and buildColor() functions in the listing on the color.html page.

The result will be a smooth transition from the original color to red (#F00 == #FF0000 == (255, 0, 0)). You can see it live on the color.html page:

The jQuery Color plugin used jQuery.cssHooks to solve this same task, but we don't take the easy way out.

I also wanted to tell you about animation prefilters, but there's no documentation, and I couldn't figure out how to use them "in real life" — though I did dig up a little info (the code can be found in the Animation function):

You can play with the example on the animate.prefilter.html page:

There isn't much to say about jQuery.Animation.tweener either, but the example turned out a bit more interesting — the code below lets you animate an object's width and height along a given diagonal:

Caution: understanding what's happening here requires 8th-grade geometry knowledge.

Working example at animate.tweener.html:

Last updated