jQuery | animate() with Examples

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.

jQuery code to show the working of animate() method:

Code #1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<html>
  
<head>
           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>

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

<html>
  
<head>
    </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>

chevron_right


Output:
Before clicking the “Click Here” button-

After clicking the “Click Here” button-



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.