hybrid kill3r Posted July 6, 2011 Share Posted July 6, 2011 I want to use JS to iterate through all the instances of images with the class name of "frame and alternate their alignment. For example, the first image with the class "frame" would be to the right, then to the left, then to the right, etc. I'm pretty noobish with javascript but managed to come up with this bit of code. I think the "logic" is right, but the actual code is probably wrong. It's not really doing anything. Here's the code: window.onload = function alignScreens(){ var screenshots = document.getElementsByClassName("frame"); var alignment = "right"; for(i=0;i<screenshots.length;i++){ if(i==0){ alignment = "right"; } if(i==1){ alignment = "left"; i = 0; } screenshots[i].align = alignment; } } If anyone could help me, it would be greatly appreciated! Thanks! Link to comment Share on other sites More sharing options...
[dx] Posted July 6, 2011 Share Posted July 6, 2011 window.onload = function alignScreens() { var screenshots = document.getElementsByClassName("frame"); var alignment = "right"; for(i=0; i<screenshots.length; i++){ if ((i%2) == 0) { // THIS WILL CHECK IS NUMBER 0, 2, 4, 6, 8, 10... screenshots[i].align = alignment; alignment = 'left'; } else { // AND THIS, IF 1, 3, 5, 7, 9... screenshots[i].align = alignment; alignment = 'right'; } }} Hope you are searching this.. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.