.animate()The .animate() method in jQuery is used to perform a custorm animation.
.animate() syntax.animate(properties[,duration][,easing][,complete]);
| Parameter | Type | Description |
|---|---|---|
properties |
PlainOject | An object of CSS properties and values that the animation will move toward. |
duration |
Number,String | the time animation running,Default 400ms(normal) fast:200ms slow:600ms |
complete |
Function | A function to execute when the animation is complete. |
easing |
String | an easing function for the transition. Default swing |
.animate() Examplesanimate any element
animate different properties of the div element simultaneously:
HTML
<div id="clickme">
Click here
</div>
<div id="container">
<div id="block">Hello!</div>
</div>
jQuery
$("#clickme").click(function(){
$("#block").animate({
width: "300px",
height: "250px",
left: "+=40",
opacity: 0.8,
borderWidth: 10
}, 1000, function(){
// Alert(Animation complete)
});
});
move the element to left or right
HTML
<button id="left">left</button><button id="right">right</button> <div id="block">Hello!</div>
jQuery
$("#right").click(function(){
$(".block").animate({left: '+50px'}, "slow");
});
$("#left").click(function(){
$(".block").animate({left: '-50px'}, "slow");
});
Animate all paragraphs to toggle both height and opacity within 600 milliseconds.
$("p").animate({
height: 'toggle', opacity: 'toggle'
}, "slow");
Animate all paragraphs to a left style of 50 and opacity of 1.
$( "p" ).animate({
left: 50, opacity: 1
}, 500 );
