jQuery animate() Method
jQuery animate() method perform custom animation using element's CSS properties.
Syntax
This syntax represent to the animate method
$(selector).animate( properties, [duration, easing, callback] );
Parameter | Type | Description |
---|---|---|
properties | Object | Required. Specifies an object of CSS properties and values to be animated. Following CSS Properties that can be animated, |
duration | Number String |
Optional. Accept string or number that determining how long animation will run. default value is 400 Accepted values:
|
easing | String | Optional. Specifies the which easing function to use for the transition. default value is "swing" Accepted values:
|
callback | Function | Optional. A function to call once the animation is complete. |
Alternative Syntax
This syntax is also acceptable for the animate method
$(selector).animate( properties, [options] );
Parameter | Type | Description |
---|---|---|
properties | Object | Required. Specifies an object of CSS properties and values to be animated. |
options | Object | Optional. Specifies additional parameters for the animation. Accepted values:
|
Examples
Following are few animate()
method examples,
- jQuery animate() - Single Property
- jQuery animate() - Multiple Properties
- jQuery animate() - Relative Values
- jQuery animate() - Queued Animations
- jQuery animate() - Pre-defined Values
jQuery animate() - Single Property
$(document).ready(function() {
$("button").click(function(){
$("div").animate({fontSize:"60px"}, 1000);
});
});
jQuery animate() - Multiple Properties
You can animate multiple properties of an element at the same time using the animate() method. All CSS properties animated without any delay.
$(document).ready(function() {
$("#btnstart").click(function(){
$("#animatebox").animate({
height: "300px",
width: "300px",
opacity: 0.5,
marginLeft: "100px",
borderWidth: "10px",
fontSize: "40px"
});
});
$("#btnstop").click(function(){
$("#animatebox").animate({
height: "100px",
width: "100px",
opacity: 1,
marginLeft: "10px",
borderWidth: "5px",
fontSize: "15px"
});
});
});
jQuery animate() - Relative Values
You can animate CSS properties by define relative values. you have to use += or -= before the value.
$(document).ready(function() {
$("#btnstart").click(function(){
$("div").animate({
height: "+=200px",
width: "+=200px"
});
});
$("#btnstop").click(function(){
$("div").animate({
height: "-=200px",
width: "-=200px"
});
});
});
jQuery animate() - Queued Animations
You can animate CSS properties one by one individually in a animate queue using jQuery chaining of an element.
$(document).ready(function() {
$("#btnstart").click(function(){
var $box = $("#animatebox");
$box.animate({height: "300px"});
$box.animate({width: "300px"});
$box.animate({opacity: 0.5});
$box.animate({marginLeft: "100px"});
$box.animate({borderWidth: "10px"});
$box.animate({fontSize: "40px"});
});
$("#btnstop").click(function(){
var $box = $("#animatebox");
$box.animate({height: "100px"});
$box.animate({width: "100px"});
$box.animate({opacity: 1});
$box.animate({marginLeft: "10px"});
$box.animate({borderWidth: "5px"});
$box.animate({fontSize: "15px"});
});
});
jQuery animate() - Pre-defined Values
You can animate of an element by using pre-defined values of property's such as "hide", "show", or "toggle".
$(document).ready(function() {
$("#btntoggle").click(function(){
$("#animatebox").animate({
width: "toggle"
});
});
});