Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

jQuery animate() method

jQuery animate() method

jQuery effects methods jQuery effects methods

Example

Apply an animation to an element by changing its height:

$

(

"

button

"

)

.

click

(

function

(

)

{

$

(

"

#box

"

)

.

animate

(

{

height

:

"

300px

"

}

)

;

}

)

;

Try it »


Definition and Usage

The animate() method performs a custom animation of a set of CSS properties.

This method changes an element from one state to another through CSS styles. CSS property values change gradually, creating the animation effect.

Only numeric values can create animations (such as "margin:30px"). String values cannot create animations (such as "background-color:red").

**Tip:** Use "+=" or "-=" to create relative animations.


Syntax

(

selector

).animate(

{styles},speed,easing,callback

)

Parameters Description
styles Required. Specifies one or more CSS properties/values to animate.

Note: When used with the animate() method, property names must be in camelCase: you must use paddingLeft instead of padding-left, marginRight instead of margin-right, and so on.

Properties that can be animated:

  • backgroundPositionX
  • backgroundPositionY
  • borderWidth
  • borderBottomWidth
  • borderLeftWidth
  • borderRightWidth
  • borderTopWidth
  • borderSpacing
  • margin
  • marginBottom
  • marginLeft
  • marginRight
  • marginTop
  • outlineWidth
  • padding
  • paddingBottom
  • paddingLeft
  • paddingRight
  • paddingTop
  • height
  • width
  • maxHeight
  • maxWidth
  • minHeight
  • minWidth
  • fontSize
  • bottom
  • left
  • right
  • top
  • letterSpacing
  • wordSpacing
  • lineHeight
  • textIndent
  • **Tip:** Color animation is not included in the core jQuery library. If you want to animate colors, you need to download the [color animation plugin](http://plugins.jquery.com/project/color) from jQuery.com. | | *speed* | Optional. Specifies the speed of the animation.

    Possible values:

    | | *easing* | Optional. Specifies the speed of the element at different points in the animation. The default is "swing".

    Possible values:

    **Tip:** More easing functions are available in extension plugins. | | *callback* | Optional. A function to be executed after the animate function finishes.
     如需学习更多有关 callback 的内容,请访问我们的 [jQuery Callback](jquery-callback.html) 这一章。 |
    
    

    Alternate syntax

    (

    selector

    ).animate(

    {styles},{options}

    )

    Parameters Description
    styles Required. Specifies one or more CSS properties/values to animate (as above).
    options Optional. Specifies additional options for the animation.

    Possible values:

    • speed - Sets the speed of the animation
    • easing - Specifies the easing function to use
    • callback - Specifies a function to execute after the animation completes
    • step - Specifies a function to execute after each step of the animation completes
    • queue - A Boolean indicating whether to place the animation in the effects queue. If false, the animation starts immediately.
    • specialEasing - A mapping of one or more CSS properties from the styles parameter and their corresponding easing functions
    • |

      Try it - Example

      Using animate() with a callback function
      How to use animate() with a callback function to repeat an animation.


      jQuery effects methods jQuery effects methods