How Do I Use Animate Method In JQuery With Example

admin_img Posted By Bajarangi soft , Posted On 13-10-2020

In jQuery animate() method is used to create custom animations.So today we discuss how to animate method

How Do I Use Animate Method In JQuery With Example

Syntax:

$(selector).animate({params},speed,callback);

The required params parameter defines the CSS properties to be animated.

The optional speed parameter specifies the duration of the effect. It can take the following values: "slow", "fast", or milliseconds.

The optional callback parameter is a function to be executed after the animation completes.

The following example demonstrates a simple use of the animate() method; it moves a <div> element to the right, until it has reached a left property of 250px:

Step 1:Create index.html file and implement below code.

<button class="btn-success">Start Animation</button><br><br><br>
<p style="background:#98bf21;height:100px;width:100px;position:absolute;"></p>


Step 2:Implement jquery to use Animate method.

<script>
    $(document).ready(function(){
        $("button").click(function(){
            $("p").animate({left: '250px'});
        });
    });
</script>


Complete Code For Animate Method In JQuery

<!DOCTYPE html>
<html>
<head>
    <title>How Do I Use Animate Method In JQuery With Example</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<style>
    body {
        background: darkolivegreen;
    }
</style>
<body>
<div class="container">
    <br><br><br>
    <div class="text-center">
        <h2 id="color" style="color: White">Animate Method In JQuery</h2>
    </div>
    <br>
    <br>

    <button class="btn-success">Start Animation</button>
    <br><br><br>
    <p style="background:#98bf21;height:100px;width:100px;position:absolute;"></p>

</div>
</body>
</html>
<script>
    $(document).ready(function () {
        $("button").click(function () {
            $("p").animate({left: '250px'});
        });
    });
</script>

 

Related Post