Domanda

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();
});
È stato utile?

Soluzione

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

Altri suggerimenti

Try below code

function cloudRight(){

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

}

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

        $(document).ready(function(){
            $(window).on('load', function () {
                cloudRight();
            });
        });
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top