Jump to content

Recommended Posts

Posted

Hi All,

 

I'm trying to animate some images using jquery. They're all stacked on top of each other then I use this code to animate,

 

var pos = "1";
$(".funnyperson").each(
                       function(){
                           $(this).animate({ "left": pos+"px", "top": "10px" }, 500);
                           pos = pos + 20;
                       }
                   );

 

My thinking been it will add 20px to each one to become unstacked and visible. This works for the first two images but the others fly off screen by miles (or its equivalent in pixels).

 

Anyone know why it's not working as expected?

 

Thanks,

Posted
Hi All,

 

I'm trying to animate some images using jquery. They're all stacked on top of each other then I use this code to animate,

 

var pos = "1";
$(".funnyperson").each(
                       function(){
                           $(this).animate({ "left": pos+"px", "top": "10px" }, 500);
                           pos = pos + 20;
                       }
                   );

 

My thinking been it will add 20px to each one to become unstacked and visible. This works for the first two images but the others fly off screen by miles (or its equivalent in pixels).

 

Anyone know why it's not working as expected?

 

Thanks,

 

Because you're initiating pos as a string with this line:

var pos="1";

 

So when you do +20, it appends it, so it goes: 1, 120, 12020, 1202020 etc.

 

It should be:

var pos = 1;
$(".funnyperson").each(
                       function(){
                           $(this).animate({ "left": pos.toString()+"px", "top": "10px" }, 500);
                           pos = pos + 20;
                       }
                   );

 

Not tested, but that's what jumps out at me :)

  • Thanks 2
Posted
Yes that was it. Can't believe I missed that. Thanks.

 

When you stare at code for long enough, it all stops making sense! A fresh pair of eyes often helps :)

Posted
When you stare at code for long enough, it all stops making sense! A fresh pair of eyes often helps :)

 

So very very true.. Sometimes when I'm stuck I walk away and do something else for an hour and then everything falls into place when I come back to it later.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...