PDA

View Full Version : Move image up the page



billyboy
01-24-2006, 02:50 AM
I found a js code that'll move an image from left to right when the image is clicked on and changed it to move an image up. But it only works in IE. What needs to be changed or added to get it to work in Firefox?

function moveit(spot){
if (document.layers){
var picture=document.all.image;
if (spot<500){
picture.bottom=spot; spot+=1; setTimeout('moveit('+spot+')',1);
}
}
if (document.all){
var picture=document.all.image.style;
if (spot<500){
picture.bottom=spot; spot+=1; setTimeout('moveit('+spot+')',1);
}
}
}

jscheuer1
01-24-2006, 06:08 AM
Hard to say without knowing what image stands for but, if it is the id of the image in question then this will do:


function moveit(spot){
if (document.layers){
var picture=document.all.image;
if (spot<500){
picture.bottom=spot; spot+=1; setTimeout('moveit('+spot+')',1);
}
}
if (document.all||document.getElementById){
var picture=document.all? document.all.image.style : document.getElementById('image').style;
if (spot<500){
picture.bottom=spot; spot+=1; setTimeout('moveit('+spot+')',1);
}
}
}

If, on the other hand, image is a variable containing a value equivalent to the id of the image, get rid of the red single quotes. If image is something else, a different approach is needed.

billyboy
01-24-2006, 09:19 AM
Thanks John, that works perfectly.
Sorry for not specifying what image was for. I would have posted the html too if I'd realized it mattered, but you were correct in assuming it was the id.

In my feeble attempts to get it to work I did some surfing and figured getElementById had to go in there somewhere, just had no idea how. Am I correct that's the standard DOM method, document.all is IE specific, and document.layers is for older versions of Netscape? If so, is document.layers really even needed, since there can't be that many people still using older versions?

jscheuer1
01-24-2006, 09:28 AM
Yes, getElementById is standard DOM and supported by all modern browsers, including IE6. The older version support never hurts. I bet there are at least 100's of thousands of old NS4 browsers still in use though. Compare that to the millions (billions?) of modern browsers, and that isn't much. Who knows? Maybe your site will attract the NS4 crowd. I once worked on a project for use in the developing world and all of that had to be IE5.0 compliant.