Skip to content

jQuery Animate

jQuery Effects - Custom Animations with animate()

Section titled “jQuery Effects - Custom Animations with animate()”

The jQuery animate() method allows you to create custom animations by gradually changing CSS properties over a specified duration.

Syntax:

$(selector).animate(properties, [duration], [easing], [complete]);

Parameters:

  • properties: (Required) An object containing CSS properties to animate and their target values. Property names must use camelCase (e.g., marginLeft, fontSize).
  • duration: (Optional) Specifies the animation speed. Can be a number in milliseconds (e.g., 1000), or the strings 'slow' (600ms) or 'fast' (200ms). Default is 400.
  • easing: (Optional) A string indicating the easing function to use for the transition (how the animation progresses over time). Default is 'swing'. Requires jQuery UI or a plugin for more options beyond 'linear' and 'swing'.
  • complete: (Optional) A callback function executed once the animation for an element is complete.

Example: Animating the left property:

// Assumes a div styled with position: relative or absolute
$("#moveRightBtn").click(function() {
$("#myBox").animate({
left: '300px' // Target value for the left property
}, 1000); // Duration: 1000ms (1 second)
});

Important: To animate positional properties (like top, left, bottom, right), the element must have a CSS position value of relative, absolute, or fixed. Static elements (the default) cannot be positioned.

You can animate multiple CSS properties simultaneously by including them in the properties object.

Example: Animating position, opacity, and size:

$("#animateAllBtn").click(function() {
$("#myBox").animate({
left: '250px',
opacity: 0.5,
height: '200px',
width: '200px',
fontSize: '24px' // Use camelCase for multi-word properties
}, 'slow', function() {
// Animation complete callback
console.log('Animation finished!');
$(this).css('border', '2px solid green'); // Example action after animation
});
});

Note on Animatable Properties: Most numeric CSS properties can be animated (e.g., dimensions, margins, padding, positions, font-size, opacity). Color animation is not supported by core jQuery’s animate() and usually requires a plugin (like jQuery UI) or is better handled using modern CSS Transitions/Animations.

Modern CSS offers powerful alternatives for animation:

  • CSS Transitions: Smoothly change property values over time when a state changes (e.g., on hover, or when a class is added/removed). Simpler for state-based changes.
  • CSS Animations (@keyframes): Define complex, multi-step animations entirely in CSS.

Advantages of CSS approaches:

  • Performance: Often smoother as they can be hardware-accelerated by the browser.
  • Separation of Concerns: Keeps presentation logic (animation) separate from behavior logic (JavaScript).
  • Simpler Syntax (for many common cases).

When to use jQuery animate():

  • Complex sequential animations or animations based on dynamic JavaScript calculations.
  • Need for fine-grained control via callbacks at specific points.
  • Compatibility with older browsers that don’t fully support CSS transitions/animations.
  • When already heavily using jQuery for other interactions.

Recommendation: Prefer CSS Transitions/Animations for most visual effects. Use jQuery animate() when its specific features or browser compatibility are required.

You can animate properties relative to their current value by prefixing the target value with += or -=.

Example: Increasing width and height:

$("#growBoxBtn").click(function() {
$("#myBox").animate({
height: '+=50px', // Increase height by 50px
width: '+=50px' // Increase width by 50px
}, 'fast');
});

For certain properties like height, width, and opacity, you can use the string values 'show', 'hide', or 'toggle' within animate().

Example: Toggling height with animation:

$("#toggleHeightBtn").click(function() {
$("#myBox").animate({
height: 'toggle'
}, 'slow');
});

This effectively animates the height between its current value and 0 (for ‘hide’) or from 0 to its default/previous height (for ‘show’). It combines the animation with the display logic.

By default, jQuery uses an animation queue (‘fx’ queue). When you call multiple animate() methods (or other effect methods like slideDown, fadeIn) on the same element consecutively, they are added to the queue and executed one after another.

Example: Sequential animations:

$("#sequentialAnimBtn").click(function() {
const $box = $("#myBox");
// Animation 1: Move right
$box.animate({ left: '+=100px' }, 'slow');
// Animation 2: Increase size (starts after move finishes)
$box.animate({ height: '+=50px', width: '+=50px' }, 'slow');
// Animation 3: Fade slightly (starts after size increase finishes)
$box.animate({ opacity: 0.7 }, 'fast');
// Animation 4: Move back (starts after fade finishes)
$box.animate({ left: '-=100px' }, 'slow');
});

This queuing behavior makes it easy to create complex animation sequences.

For a complete overview of all jQuery effects, including animate() options and other animation methods, please visit the official jQuery Effects documentation.