Skip to content
Related Articles
Open in App
Not now

Related Articles

How to animates first div left property and synchronizes the remaining divs ?

Improve Article
Save Article
  • Last Updated : 17 Feb, 2022
Improve Article
Save Article

In this article, we will learn how to animate a division element’s left property in sync with the other division elements. This can be used in situations where multiple division elements have to be repositioned or animated using only one property.

Approach: We will be using the jQuery click(), animate(), gt() and css() methods. Each element will that has to be animated will have a class of “gfg-animate”. 

A button with a class of “animation-btn” is defined which on click will trigger the animate() method applied to the first division element. The animate() method consists of two parameters. The first parameter contains the styling of the left property which is modified to a new value. The second parameter sets the speed of the parameter using the duration property defined within this parameter. 

Next, the step function is used to animate the left property of all the division elements with respect to the left property of the first division element. The left property of the division elements is modified using the css() method within the step function. All the division elements are selected using the gt() method with the index being specified as 0.

Note: Remember to set the position as relative of all the division elements since each one has to be synchronized with respect to the first division element.

Example: In this example, all the division elements are animated for a duration of three seconds in accordance with the first division element’s left property which is 250.


<!DOCTYPE html>
    <script src=
    <!-- Basic inline styling -->
        body {
            text-align: center;
        h1 {
            color: green;
            font-size: 40px;
        /* Necessary for animation to work */
        .gfg-animate {
            position: relative;
            display: inline-block;
            background-color: green;
            width: 45px;
            height: 45px;
            margin-right: 0.5rem;
        button {
            cursor: pointer;
            margin: 0 auto;
            display: block;
            margin-top: 2rem;
    <div class="gfg-animate"></div>
    <div class="gfg-animate"></div>
    <div class="gfg-animate"></div>
    <div class="gfg-animate"></div>
    <button class="animation-btn">
        Click to execute animation
    <script type="text/javascript">
        $(".animation-btn").click(function () {
            // Get the first division element
            // and animate it
                    left: 250,
                    duration: 3000,
                    step: function (param) {
                            .css("left", param);


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!