jQuery Animations



index
Disabled back button Next Section
printable version

Section 1: Introduction

The jQuery library provides several techniques for adding animation to a web page, allowing developers to craft sophisticated custom effects.

The basic (built-in) animation effects include

Section 2: Using Effects

Basic Animation Signature

Based on the following:

Animation signature.

Using Hide and Show

Animation hide/show. Animation hide/show.

Demo of hide/show


Using SlideUp and SlideDown

Animation SlideUp and SlideDown. Animation SlideUp and SlideDown.

Demo of slide up / slide down


Using FadeIn and FadeOut

Animation FadeIn and FadeOut. Animation FadeIn and FadeOut.

Demo of fade in / fade out


Using FadeTo

Animation FadeTo. Animation FadeTo.

Demo of fade to


Using Toggle

Animation Toggle. Animation Toggle.

Demo of toggle


Completed parameter

The previous examples demonstrated action and duration, and now we'll look at completed.

completed is the method that will run when the animation has completed, and a function can be defined here.

Animation Completed. Animation Completed.

Demo of toggle with completed

Section 3: Animate Function

The previous examples used the basic action signature, but the focus now turns to the animate() function.

The animate() function is used to create custom animations, typically by animating various CSS properties.

The animate() function signature follows:

Animate function.

Examples

In the following demo, a custom animation makes the image appear to pop, by altering both the width and margin-left CSS properties , with a duration set to 1 second and the easing parameter set to swing.

Animation Figure.

Demo of animate function


Here is the critical code for the demo:

Animation Code.
Section 4: CSS Animations and Transitions

Animations are used to animate the values of CSS properties over time, using keyframes.

Transitions are used to perform animations.


Example

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Testing CSS Animations</title>
<style>
#ani, #trans {height: 100px;width: 100px;background-color: #F00;}
#ani:hover {animation: changeColor linear 3s;}
@keyframes changeColor {
from {background-color: #F00;}
to {background-color: #00F;}
}
#trans {
transition: background-color linear .5s, width ease-in-out 3s;
}
#trans:hover {
background-color: #00F;width: 500px;
}
</style>
</head>
<body>
<p>Hover over each square to see CSS animation.</p>
<div id="ani"></div>
<div id="trans"></div>
</body>
</html>

Demo of CSS animation

Note that jQuery is not needed for pure CSS animations.


Syntax

The two forms of syntax below work the same:

CSS Animation Syntax.

A discussion can be found at CSS Tricks: animation

Similarly, the second example below is a shorthand version of the first:

CSS Animation Syntax.