Cover Image for jQuery animate() method
77 views

jQuery animate() method

The jQuery animate() method is used to create custom animations on selected elements. It allows you to change CSS properties of elements gradually over a specified duration, creating smooth and visually appealing animations.

The syntax for using the animate() method is as follows:

$(selector).animate(properties, duration, [easing], [callback]);
  • selector: It is a string that specifies the elements to be selected.
  • properties: It is an object that defines the CSS properties and values to be animated. Each property represents a CSS property, and its value is the target value to be reached by the animation.
  • duration: It is a string or a number representing the duration of the animation. It can take values like “slow,” “fast,” or a numeric value in milliseconds, specifying how long the animation should take to complete.
  • easing (optional): It is a string that specifies the easing function used for the animation. Easing functions control the speed of the animation at different points in time, creating different effects. Some common values include “swing” and “linear.” If you omit this parameter, the default easing function is used.
  • callback (optional): It is a function that will be executed after the animation is complete.

Here’s an example of how you can use the animate() method:

HTML:

<div id="myDiv" style="width: 100px; height: 100px; background-color: red;"></div>
<button id="animateButton">Animate Div</button>

JavaScript:

// Attach a click event handler to the button
$('#animateButton').click(function() {
  // Animate the div to change its width, height, and background color
  $('#myDiv').animate({
    width: '200px',
    height: '200px',
    backgroundColor: 'blue'
  }, 1000); // Animation duration is 1000 milliseconds (1 second)
});

In the example, when the button with the ID “animateButton” is clicked, the animate() method is used to animate the div element with the ID “myDiv.”

The animate() method takes an object with the CSS properties to be animated and their target values. In this case, the width and height properties are animated to change from 100px to 200px, and the background color is animated to change from red to blue.

The animation duration is set to 1000 milliseconds (1 second) using the duration parameter, indicating how long the animation should take to complete.

The animate() method allows you to create various types of animations, such as sliding effects, fading effects, and custom property animations. You can animate multiple properties simultaneously, and you can use easing functions to customize the animation’s acceleration and deceleration.

Additionally, you can use the optional callback function to execute code after the animation is complete, allowing you to perform actions or updates related to the animation event. The animate() method is a powerful tool for creating interactive and dynamic web page elements with smooth and engaging animations.

YOU MAY ALSO LIKE...

The Tech Thunder

The Tech Thunder

The Tech Thunder


COMMENTS