While fading and sliding methods are great for basic tasks, the animate()
method gives you total creative freedom. It allows you to create custom animations for
almost any numeric CSS property on your page.
The animate() method follows a specific pattern where you define the target
CSS properties in an object.
$(selector).animate({params}, speed, callback);
To move or resize an element, simply define the final values you want:
$("button").click(function() {
$("#box").animate({
left: '250px',
opacity: '0.5',
height: '150px',
width: '150px'
});
});
left, top, etc.), you must first set the CSS
property position to relative, fixed, or
absolute.
Sometimes you don't know the exact pixel value, but you want to change it **relative** to
its current size. You can do this by adding += or -= before
the value.
$("button").click(function() {
$("#box").animate({
left: '+=50px',
width: '+=20px'
});
});
You can even use special strings like "show", "hide", or
"toggle" as property values to create combined animation logic.
$("button").click(function() {
$("#box").animate({
height: 'toggle'
});
});
You can animate any CSS property that uses **numeric values**. Here are some common properties:
font-size becomes fontSize, and margin-left
becomes marginLeft.
If you write multiple animate() calls one after another for the same
element, jQuery creates an internal **queue**. It will finish the first animation before
starting the second.
var box = $("#box");
box.animate({height: '300px', opacity: '0.4'}, "slow");
box.animate({width: '300px', opacity: '0.8'}, "slow");
box.animate({height: '100px', opacity: '0.4'}, "slow");
box.animate({width: '100px', opacity: '0.8'}, "slow");