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

jQuery Effects animate() Method

Métodos HTML/CSS do jQuery

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

Animations make it possible to transition smoothly 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 other than "show", "hide", and "toggle" cannot be animated (e.g., background color). These values allow for hiding and showing animated 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 attribute.

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 the animation of an element by changing its 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:

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

Use animate() together with a callback function:

  $("div").animate({
    width:"500px",
    height:"400px"
  }, 500, "linear",
  function() {
    $(this).after("

动画完成

");   }); });
测试看看‹/›

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

  $("div").animate({
    width:"500px",
    height:"400px"
  
    duration:500,
    easing:"linear",
    complete:function(){
      $(this).after("

动画完成

");     }   }); });
测试看看‹/›

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

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

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

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

  $\(\"div\\
});
测试看看‹/›

参数值(语法1)

$\(selector\).animate\({styles},\ duration,\ easing,\ callback\)
参数Parâmetros
DeObrigatório. Especifica um ou mais atributos CSS que produzem o efeito de animação/值。
注意:与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}\)
参数Parâmetros
DeObrigatório. Especifica um ou mais atributos CSS que produzem o efeito de animação/Descrição
Valor (igual ao acima).options

(Opcional)Especificar opções adicionais para a animação

  • Opcional:

  • duration Uma string ou número que determina quanto tempo a animação deve durar

  • easing Uma string que indica qual função de suavização deve ser usada na transição -complete

  • Função chamada após a conclusão da animação -step

  • Função chamada para cada atributo de animação de cada elemento de animação -progress

  • Função a ser executada após cada passo da animação-queue

  • Um valor booleano que especifica se a animação deve ser colocada na fila de efeitos -specialEasingDestyles

  • Mapeamento de um ou mais atributos CSS e suas funções de suavização correspondentes -start

  • Função a ser executada quando a animação começar -done

  • Função a ser executada quando a animação terminar -fail

  • Função a ser executada se a animação não puder ser concluída -always

Métodos HTML/CSS do jQuery