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

jQuery effects – animations

jQuery effects- animations


The jQuery animate() method allows you to create custom animations.

jQuery


jQuery animation - animate() method

The jQuery animate() method is used to create custom animations.

Syntax:

$(

selector

).animate({

params

}

,speed,callback

);

The required params parameter defines the CSS properties that form the animation.

The optional speed parameter specifies the duration of the effect. It can take the values "slow", "fast" or milliseconds.

The optional callback parameter is the name of the function to be executed after the animation completes.

The following example demonstrates a simple use of the animate() method. It moves the

element 250 pixels to the right:

Example

$

(

"

button

"

)

.

click

(

function

(

)

{

$

(

"

div

"

)

.

animate

(

{

left

:

'

250px

'

}

)

;

}

)

;

Try it »

| lamp | By default, all HTML elements have a static position and cannot be moved.
To manipulate position, remember to first set the element's CSS position property to relative, fixed or absolute! | | --- | --- |


jQuery animate() - manipulating multiple properties

Note that multiple properties can be animated at the same time:

Example

$

(

"

button

"

)

.

click

(

function

(

)

{

$

(

"

div

"

)

.

animate

(

{

left

:

'

250px

'

,

opacity

:

'

0.5

'

,

height

:

'

150px

'

,

width

:

'

150px

'

}

)

;

}

)

;

Try it »

| lamp | Can you animate all CSS properties with the animate() method?

 

是的,几乎可以!不过,需要记住一件重要的事情:当使用 animate() 时,必须使用 Camel 标记法书写所有的属性名,比如,必须使用 paddingLeft 而不是 padding-left,使用 marginRight 而不是 margin-right,等等。

同时,色彩动画并不包含在核心 jQuery 库中。

如果需要生成颜色动画,您需要从 jquery.com 下载 颜色动画 插件。

|

| --- | --- |


jQuery animate() - using relative values

You can also define relative values (relative to the element's current value). Add += or -= before the value:

Example

$

(

"

button

"

)

.

click

(

function

(

)

{

$

(

"

div

"

)

.

animate

(

{

left

:

'

250px

'

,

height

:

'

+=150px

'

,

width

:

'

+=150px

'

}

)

;

}

)

;

Try it »


jQuery animate() - using predefined values

You can even set an animated property's value to "show", "hide" or "toggle":

Example

$

(

"

button

"

)

.

click

(

function

(

)

{

$

(

"

div

"

)

.

animate

(

{

height

:

'

toggle

'

}

)

;

}

)

;

Try it »


jQuery animate() - using the queue feature

By default, jQuery provides a queue feature for animations.

This means that if you write multiple animate() calls one after another, jQuery creates an "internal" queue containing those method calls. It then runs the animate calls one by one.

Example 1

$

(

"

button

"

)

.

click

(

function

(

)

{

var

div

=$

(

"

div

"

)

;

div

.

animate

(

{

height

:

'

300px

'

,

opacity

:

'

0.4

'

}

,

"

slow

"

)

;

div

.

animate

(

{

width

:

'

300px

'

,

opacity

:

'

0.8

'

}

,

"

slow

"

)

;

div

.

animate

(

{

height

:

'

100px

'

,

opacity

:

'

0.4

'

}

,

"

slow

"

)

;

div

.

animate

(

{

width

:

'

100px

'

,

opacity

:

'

0.8

'

}

,

"

slow

"

)

;

}

)

;

Try it »

The following example moves the

element 100 pixels to the right, then increases the text font size:

Example 2

$

(

"

button

"

)

.

click

(

function

(

)

{

var

div

=$

(

"

div

"

)

;

div

.

animate

(

{

left

:

'

100px

'

}

,

"

slow

"

)

;

div

.

animate

(

{

fontSize

:

'

3em

'

}

,

"

slow

"

)

;

}

)

;

 

Try it »