Chipe
Chipe

Reputation: 4801

animation to repeat after it is done?

This animation runs when after the page loads. After it completes the animation, how can I have it repeat again and again? Thanks for any help!

function cloudRight(){
$(window).on('load', function () {
    $( ".cloudRight" ).animate({
        left: "+=1000px",
    }, 30000, "swing", 
    function() {
        $( ".cloudRight" ).animate({
            left: "-=1000px",
        }, 30000, "linear");
    }); 
  }); 
}

$(document).ready(function(){
    cloudRight();
});

Upvotes: 0

Views: 104

Answers (3)

A. Wolff
A. Wolff

Reputation: 74420

You could use:

function cloudRight() {
    $(".cloudRight").animate({
        left: "+=1000px",
    }, 30000, "swing",

    function () {
        $(this).animate({
            left: "-=1000px",
        }, 30000, "linear", cloudRight); // call it again on animation complete
    });
}

$(cloudRight); // call on document ready

If there is more than one element with class cloudRight, you should use a promise instead, to recall it only once each time:

function cloudRight() {
    $(".cloudRight").animate({
        left: "+=1000px",
    }, 30000, "swing",

    function () {
        $(this).animate({
            left: "-=1000px",
        }, 30000, "linear");
    }).promise().done(cloudRight);
}

--DEMO--

Upvotes: 3

yeswanth
yeswanth

Reputation: 1549

        function cloudRight(){
            $( ".cloudRight" ).animate({
                left: "+=30px",
            }, 300, "swing", 
            function() {
                $( ".cloudRight" ).animate({
                    left: "-=30px",
                 }, 300, "linear");
                 cloudRight();
            });
        }

        $(document).ready(function(){
            $(window).on('load', function () {
                cloudRight();
            });
        });

Upvotes: 1

siraj pathan
siraj pathan

Reputation: 1495

Try below code

function cloudRight(){

    $( ".cloudRight" ).animate({
        left: "+=1000px",
    }, 30000, "swing", 
    function() {
        $( ".cloudRight" ).animate({
            left: "-=1000px",
        }, 30000, "linear",cloudRight);
    }); 

}

$(document).ready(function(){
    cloudRight();
});

Upvotes: 1

Related Questions