Latest web development tutorials

jQuery animate () method

jQuery Effect Methods jQuery Effect Methods

Examples

By changing the height of an element, the element of animation application:

$("button").click(function(){
$("#box").animate({height:"300px"});
});

try it"

Definition and Usage

animate () method to perform a custom animation CSS attribute set.

In this method, the CSS style elements to change from one state to another. CSS property value is gradually changing, so you can create animation effects.

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

Tip: Use the "+ =" or "- =" to create a relative animation.


grammar

(selector).animate({styles},speed,easing,callback)

参数 描述
styles 必需。规定产生动画效果的一个或多个 CSS 属性/值。

注意: 当与 animate() 方法一起使用时,该属性名称必须是驼峰写法: 您必须使用 paddingLeft 代替 padding-left,marginRight 代替 margin-right,依此类推。

可以应用动画的属性:

提示:颜色动画不包含在核心 jQuery 库中。如果您想要应用动画颜色,您需要从 jQuery.com 下载 颜色动画插件
speed 可选。规定动画的速度。

可能的值:

  • 毫秒
  • "slow"
  • "fast"
easing 可选。规定在动画的不同点中元素的速度。默认值是 "swing"。

可能的值:

  • "swing" - 在开头/结尾移动慢,在中间移动快
  • "linear" - 匀速移动
提示:扩展插件中提供更多可用的 easing 函数。
callback 可选。animate 函数执行完之后,要执行的函数。

如需学习更多有关 callback 的内容,请访问我们的 jQuery Callback 这一章。


Alternate Syntax

(selector).animate({styles},{options})

参数 描述
styles 必需。规定产生动画效果的一个或多个 CSS 属性/值(同上)。
options 可选。规定动画的额外选项。

可能的值:

  • speed - 设置动画的速度
  • easing - 规定要使用的 easing 函数
  • callback - 规定动画完成之后要执行的函数
  • step - 规定动画的每一步完成之后要执行的函数
  • queue - 布尔值。指示是否在效果队列中放置动画。如果为 false,则动画将立即开始。
  • specialEasing - 来自styles参数的一个或多个 CSS 属性的映射,以及它们的对应 easing 函数


Examples

Try - Example

Use animate with callback function ()
How to use with a callback function animate () to repeat the animation.


jQuery Effect Methods jQuery Effect Methods