English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
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).
$\(selector\).animate\({styles},\ duration,\ easing,\ callback\)
$\(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\\ });测试看看‹/›
$\(selector\).animate\({styles},\ duration,\ easing,\ callback\)
参数 | Parâmetros |
---|---|
De | Obrigatório. Especifica um ou mais atributos CSS que produzem o efeito de animação/值。 注意:与animate()方法一起使用时,属性名称必须为驼峰式:是paddingTop而不是padding-top,marginLeft而不是margin-left以及依此类推 |
duration | (可选)确定动画将运行多长时间的字符串或数字。预设值为400毫秒 可能的值:
|
easing | (可选)指定在动画的不同点中元素的速度。默认值是 "swing"。 可能的值:
|
callback | (可选)animate 函数执行完之后,要执行的函数。 |
$\(selector\).animate\({styles},\ {options}\)
参数 | Parâmetros |
---|---|
De | Obrigató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
|