简体   繁体   中英

How to make a background animation into a loop (repeat function)

I have a script showing starts on the background of the website. It stops after a while. How can I make it into a loop?

$(function(){
  $('#midground').css({backgroundPosition: '0px 0px'});
  $('#foreground').css({backgroundPosition: '0px 0px'});

  $('#midground').animate({
    backgroundPosition:"(-10000px -2000px)"
  }, 240000, 'linear');

  $('#foreground').animate({
    backgroundPosition:"(-10000px -2000px)"
  }, 180000, 'linear');
})

Stick it in a function then call itself after a setTimeout.

function myAnimation() {
    $('#midground').css({backgroundPosition: '0px 0px'});
    $('#foreground').css({backgroundPosition: '0px 0px'});

        $('#midground').animate({
            backgroundPosition:"(-10000px -2000px)"
        }, 240000, 'linear');

        $('#foreground').animate({
            backgroundPosition:"(-10000px -2000px)"
        }, 180000, 'linear', function() {
            setTimeout(myAnimation, 500);
        });
    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM