Reputation: 4801
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
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);
}
Upvotes: 3
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
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