0

I'm trying to do something very simple: continuously loop a background image. I'm doing this via the "Background Position Animations" plugin.

My current code:

$(function skyloop() {  
  $('#header').animate ({backgroundPosition:"(-250 0)"}, {duration:1000});  
});

I need a way to call the 'skyloop' function at the end of the animation, which would cause it to loop, but I can't seem to find the way to do that.

[EDIT] I've read all of the replies and I can't figure it out; nothing seems to work. It simply refuses to run again. The only thing I can think of is that the issues is caused by the plugin as I'm modifying a background image. I'll try to find another way to move a background image continuously (I'm trying to simulator a sky, by the way).

8 Answers 8

3

There's a callback parameter for animate: http://api.jquery.com/animate/

.animate( properties, [ duration ], [ easing ], [ callback ] )

EDIT:

Your problem in this case (as well as needing the callback) is the backgroundPosition:(-250 0), instead it should be using relative operators to keep decrementing the background position, like so: backgroundPosition: "-=250".

Sign up to request clarification or add additional context in comments.

2 Comments

+1 for linking to the documentation and referencing that primarily (in my opinion, the most reliable and beneficial method of learning in the long term -- understanding and working with references), but you should maybe also add a part to your answer that directly answers the OP's question (to distinguish this answer from any other question referring to having a callback to jQuery's animate).
Aye. I did take a look at the documentation, and tried to add the simple callback at the end but it simply did not work. I'm starting to think that it is likely cause by the plugin I'm using to animate the background.
2
setInterval( "skyloop()", 1001);
function skyloop() {   
  $('#header').animate ({backgroundPosition:"(-250 0)"}, {duration:1000});   
}; 

Comments

2

demo

function skyloop() {  
  $('#header').animate ({"background-position":"-=250"}, 1000, skyloop);  
}

Comments

2
$(function(){
   var skyloop = function(){
      $('#header')
          .animate({backgroundPosition:"(-250 0)" }, { duration: 1000 })
          .queue(skyloop);
   }
});

queue adds a function to fire at the end of your animation.

Comments

1

You can do it like this:

function skyloop() {  
  $('#header').animate({backgroundPosition:"(-250 0)"}, 
                       {duration:1000}, 
                       skyloop);  
}
$(skyloop); //call it on document.ready to kick it off

Comments

0

I think the easiest way is to have a look at jQuery Timers: http://plugins.jquery.com/project/timers

For example the everyTime() function.

Comments

0

You do not need that plugin. Use a more recent jQuery version that supports the .animate. (1.4.2 is the latest)

The animate method supports callback functions when the current animation completes..

Comments

0

Although it might look better to use the .animate method to scroll your background; you might start to see high cpu usage once its running. Depending on the background image you might want to check out jquery-randomised-background-scrolling-effect-tutorial. You could take a sky picture and scroll that insteal

But back to the question at hand one way to handle relooping the JS setInterval should have done the trick. although this is how I would have wrote it.

setInterval(function(){ $('#header').animate ({backgroundPosition:"(-250 0)"}, {duration:1000});} , 1000);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.