9951 explained code solutions for 126 technologies


jqueryHow can I use jQuery animate to create animations in my website?


jQuery animate() is a powerful method for creating animations on a website. It allows you to animate the CSS properties of an element over a given duration. Here is an example of how to use it:

$('#myElement').animate({
    opacity: 0.25,
    left: '+=50',
    height: 'toggle'
}, 5000);

This code will animate the element with id myElement over 5 seconds. The properties being animated are opacity, left and height. The opacity will be changed to 25%, the left property will be increased by 50px, and the height will be toggled.

The animate() method takes two parameters. The first is an object containing the CSS properties that will be animated. The second is the duration of the animation in milliseconds.

Code explanation

  • $('#myElement'): This is a jQuery selector that selects the element with id myElement.
  • animate({ ... }): This is the jQuery animate method that takes an object of CSS properties and a duration as parameters.
  • opacity: 0.25: This is a CSS property that is being animated. It will change the opacity of the element to 25%.
  • left: '+=50': This is a CSS property that is being animated. It will increase the left property of the element by 50px.
  • height: 'toggle': This is a CSS property that is being animated. It will toggle the height of the element.
  • 5000: This is the duration of the animation in milliseconds.

Helpful links

Edit this code on GitHub