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 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).
$(selector).animate({styles}, duration, easing, callback)
$(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"}); });测试看看‹/›
$(selector).animate({styles}, duration, easing, callback)
Parameters | Description |
---|---|
styles | Required. Specify one or more CSS properties that will produce the animation effect/值。 注意:与animate()方法一起使用时,属性名称必须为驼峰式:是paddingTop而不是padding-top,marginLeft而不是margin-left以及依此类推 |
duration | (可选)确定动画将运行多长时间的字符串或数字。预设值为400毫秒 可能的值:
|
easing | (可选)指定在动画的不同点中元素的速度。默认值是 "swing"。 可能的值:
|
callback | (可选)animate 函数执行完之后,要执行的函数。 |
$(selector).animate({styles}, {options})
Parameters | Description |
---|---|
styles | Required. 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:
|