English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

jQuery animate() method

jQuery Effect Methods

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

Animations make it possible to transition from one CSS style configuration to another.

All animation properties should be set to a single value (e.g., width, height, or left value).

String values cannot be animated except for the strings "show", "hide", and "toggle". These values allow for hiding and showing animation elements.

Animation properties can also be relative. If a leading+= or-= character sequence, the target value is calculated by adding or subtracting the given number from the current value of the property.

In addition to style properties, animations can also be applied to certain non-style properties (e.g., scrollTop and scrollLeft).

Syntax1:

$(selector).animate({styles}, duration, easing, callback)

Syntax2:

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

示例

Set animations for elements by changing their width:

$("#btn1).click(function(){
  $("div").animate({width: "500px");
});
测试看看‹/›

Animate elements by changing their width and height:

$("#btn1).click(function(){
  $("div").animate({width: "500px");
  $("div").animate({height: "400px");
});
测试看看‹/›

Animate elements by passing multiple style properties and values:

$("#btn1).click(function(){
  $("div").animate({
     width:"500px",
     height:"400px"
  });
});
测试看看‹/›

Use animate() withofduration and easingofParameters:

$("button").click(function(){
  $("div").animate({width:"500px", height:"400px"}, 4000, "linear");
});
测试看看‹/›

Use animate() along with a callback function:

$("button").click(function(){
  $("div").animate({
    width:"500px",
    height:"400px"
  }, 500, "linear",
  function(){
    $(this).after("<h2>动画完成</h2">
  });
});
测试看看‹/›

Use alternative syntax to specify multiple animations {styles} and {options}:

$("button").click(function(){
  $("div").animate({
    width:"500px",
    height:"400px"
  
    duration:500,
    easing:"linear",
    complete:function(){
      $(this).after("<h2>动画完成</h2">
    }
  });
});
测试看看‹/›

用户滚动页面时添加平滑滚动:

let size = $(".main").height(); // 获取".main" 高度
$(window).keydown(function(event) {
  if(event.which === 40) { // else if(event.which ===
    $("html, body").animate({scrollTop: "+=" + size}, 300);
  } 38) { // 如果按下向上箭头键
    $("html, body").animate({scrollTop: "-=" + size}, 300);
  }
});
测试看看‹/›

使用相对值为所有段落设置动画:

$("p").click(function(){
  $(this).animate({
    fontSize: "+=5px",
    padding : "+=10px"
  });
});
测试看看‹/›

此外,我们甚至可以指定属性的动画值"show","hide"或"toggle":

$("button").click(function(){
  $("div").animate({height: "toggle"});
});
测试看看‹/›

参数值(语法1)

$(selector).animate({styles}, duration, easing, callback)
ParametersDescription
stylesRequired. Specify one or more CSS properties that will produce the animation effect/值。
注意:与animate()方法一起使用时,属性名称必须为驼峰式:是paddingTop而不是padding-top,marginLeft而不是margin-left以及依此类推
duration(可选)确定动画将运行多长时间的字符串或数字。预设值为400毫秒

可能的值:

  • 毫秒(例如100、500、2000等)

  • “fast”

  • “slow”

easing(可选)指定在动画的不同点中元素的速度。默认值是 "swing"。

可能的值:

  • “swing”-在开始/结束时移动较慢,而在中间移动较快

  • “linear”-以恒定速度移动

callback(可选)animate 函数执行完之后,要执行的函数。

参数值(语法2)

$(selector).animate({styles}, {options})
ParametersDescription
stylesRequired. Specify one or more CSS properties that will produce the animation effect/Values (same as above).
options(Optional) Specify additional options for the animation

Optional value:

  • duration A string or number determining how long the animation will run

  • easing A string indicating which easing function to use in the transition

  • complete -Function called after the animation is completed

  • step -Function called for each animation property of each animation element

  • progress -Functionality to execute after each step of the animation

  • queue-A boolean value specifying whether the animation should be placed in the effect queue

  • specialEasing -FromstylesA mapping of one or more CSS properties and their corresponding easing functions

  • start -Functionality to execute when the animation starts

  • done -Functionality to execute when the animation ends

  • fail -Functionality to execute if the animation cannot be completed

  • always -Functionality to execute if the animation stops but has not completed

jQuery Effect Methods