Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

jQuery | animate() with Examples

  • Last Updated : 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:

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

(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:






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

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




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!