Jump to content

Simple Jquery Animations


dweb

Recommended Posts

Hi

 

I have the following jQuery code

 

$(document).ready(function()

{
function slider(panel)
{
jQuery("#"+panel).animate({ top: '-=30' }, 400);
}
setTimeout(slider('panel1'), 400);
setTimeout(slider('panel2'), 800);
setTimeout(slider('panel3'), 1200);
}

 

But it seems to run straight away and the inital delay doesn't seem to happen, and it also only seems to only animate "panel1" and ignores "panel2" and "panel3"

 

Any idea why this might be happening?

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/269348-simple-jquery-animations/
Share on other sites

But it seems to run straight away and the inital delay doesn't seem to happen

 

The way you wrote your setTimeout calls, you are telling JS to "execute the slider function NOW, then register what it returns to be executed LATER".

 

To run the slider function later, you need to wrap it in another function. You can just use an anonymous function for that:

setTimeout(function(){ slider('panel1'); }, 400);
setTimeout(function(){ slider('panel2'); }, 800);
setTimeout(function(){ slider('panel3'); }, 1200);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.