Método jQuery animate()
Exemplo
"Animar" um elemento, alterando sua altura:
$("button").click(function(){
$("#box").animate({height: "300px"});
});
Definição e uso
O método animate() executa uma animação personalizada de um conjunto de propriedades CSS.
Este método muda um elemento de um estado para outro com estilos CSS. O valor da propriedade CSS é alterado gradualmente, para criar um efeito animado.
Apenas valores numéricos podem ser animados (como "margin:30px"). Os valores de string não podem ser animados (como "background-color:red"), exceto para as strings "show", "hide" e "toggle". Esses valores permitem ocultar e mostrar o elemento animado.
Dica: Use "+=" ou "-=" para animações relativas.
Sintaxe
(selector).animate({styles},speed,easing,callback)
Parameter | Description |
---|---|
styles | Required. Specifies one or more CSS properties/values to animate. Note: The property names must be camel-cased when used with the animate() method: You will need to write paddingLeft instead of padding-left, marginRight instead of margin-right, and so on. Properties that can be animated: Only numeric values can be animated (like "margin:30px"). String values cannot be animated (like "background-color:red"), except for the strings "show", "hide" and "toggle". These values allow hiding and showing the animated element. |
speed | Optional. Specifies the speed of the animation. Default value is 400 milliseconds Possible values:
|
easing | Optional. Specifies the speed of the element in different points of the animation. Default value is "swing". Possible values:
|
callback | Optional. A function to be executed after the animation completes. To learn more about callback, please read our jQuery Callback chapter |
Sintaxe alternativa
(selector).animate({styles},{options})
Parameter | Description |
---|---|
styles | Required. Specifies one or more CSS properties/values to animate (See possible values above) |
options | Optional. Specifies additional options for the animation. Possible values:
|
Experimente você mesmo - Exemplo
Como usar animate() com uma função de retorno de chamada que repete a animação.
Usando a sintaxe alternativa para especificar vários estilos e opções de animação.
de progresso Como usar o método animate() para criar uma barra de progresso.
Como usar animate() para adicionar rolagem suave aos links.