jQuery | animate() with Examples

13 Feb, 2019
The animate() is an inbuilt method in jQuery which is used to change the state of the element with CSS style. This method can also be used to change the CSS property to create the animated effect for the selected element.
Syntax:
(selector).animate({styles}, para1, para2, para3);

Here “selector” is the selected element.Parameter: It accepts four parameters which are specified below-
Styles: It helps to set new css property.
para1: It is optional parameter and used to set the speed of the parameter and its default value is 400 millisecond.
para2: It is optional and this specifies the speed of the element at different position.
para3: It is optional function that is used to be perform after animation is complete.
Return Value: It returns the change that made by using the above method.
<html>  <head>    <script src="https://ajax.googleapis.com/ajax/libs/           jquery/3.3.1/jquery.min.js"></script>    <script>        <!-- jQuery code to show animate() method -->        $(document).ready(function() {            $("#b1").click(function() {                $("#box").animate({                    width: "300px"                });                $("#box").animate({                    height: "300px"                });            });        });    </script>    <style>        div {            width: 100px;            height: 100px;            background-color: green;        }                  #b1 {            margin-top: 10px;        }    </style></head>  <body>    <div id="box"></div>    <!-- click on this button -->    <button id="b1">Click Here !</button></body>  </html>
Output:Before clicking the “Click Here !” button-
After clicking the “Click Here !” button-
Code #2:In the below code, all the parameter is passed to this method.
<html>  <head>    <script src="https://code.jquery.com/jquery-1.10.2.js">    </script>    <style>        div {            background-color: green;            height: 100px;            width: 100px;            margin-top: 10px;        }                  #b1 {            margin-top: 10px;        }    </style></head>  <body>    <div id="box"></div>    <!-- click here and animation will start -->    <button id="b1">Click Here !</button>    <!-- jQuery code to show the animate method -->    <script>        $(document).ready(function() {            $("#b1").click(function() {                $("#box").animate({                    height: "200px",                    width: "200px"                }, {                    duration: 1000,                    easing: "linear",                    complete: function() {$(this).after("<p>Reaches to maximum height and width !</p>");                    }                });            });        });    </script></body>  </html>
Output:Before clicking the “Click Here” button-
After clicking the “Click Here” button-
jQuery-Effects
JavaScript
JQuery
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
Remove elements from a JavaScript Array
Hide or show elements in HTML using display property
Roadmap to Learn JavaScript For Beginners
JQuery | Set the value of an input text field
How to change selected value of a drop-down list using jQuery?
Form validation using jQuery
How to add options to a select element using jQuery?
How to Dynamically Add/Remove Table Rows using jQuery ?