bladedanny Posted March 13, 2015 Posted March 13, 2015 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,
LosOjos Posted March 13, 2015 Posted March 13, 2015 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 2
bladedanny Posted March 13, 2015 Author Posted March 13, 2015 (edited) Yes, that makes sense. I'll give it a go. --edit-- Yes that was it. Can't believe I missed that. Thanks. Edited March 13, 2015 by bladedanny
LosOjos Posted March 13, 2015 Posted March 13, 2015 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
spadam Posted March 13, 2015 Posted March 13, 2015 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.
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now