Closed Thread Icon

Preserved Topic: Getting position of an IMAGE. (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=18006" title="Pages that link to Preserved Topic: Getting position of an IMAGE. (Page 1 of 1)" rel="nofollow" >Preserved Topic: Getting position of an IMAGE. <span class="small">(Page 1 of 1)</span>\

 
Carnage
Nervous Wreck (II) Inmate

From:
Insane since: May 2001

posted posted 06-09-2001 21:05

Is there any way to get the coordinates of an image that is somewhere in a HTML page, and the image can be anywhere at any time due to resize and the like?

Thanks,

Carnage

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 06-09-2001 23:45

Funny you ask, I just recently developed a bit of JavaScript to do just that.

If the image has id="myimage" or something like that, then these functions will work:

function getTop(theelement)
{
&nbsp;&nbsp;&nbsp;&nbsp;if (theelement.offsetParent) return theelement.offsetTop + getTop(theelement.offsetParent);
&nbsp;&nbsp;&nbsp;&nbsp;else return theelement.offsetTop;
}
function getLeft(theelement)
{
&nbsp;&nbsp;&nbsp;&nbsp;if (theelement.offsetParent) return theelement.offsetLeft + getLeft(theelement.offsetParent);
&nbsp;&nbsp;&nbsp;&nbsp;else return theelement.offsetLeft;
}

Use them like this:

var imagetop = getTop("myimage");

These will work in IE and Mozilla, I'm not really sure if they're supported by the w3c standards, but since they work in Mozilla, I bet they are. This will *not* work in Netscape Navigator 4.x, and I think it's safe to say that there's no way to do it in that browser at all.

Carnage
Nervous Wreck (II) Inmate

From:
Insane since: May 2001

posted posted 06-10-2001 06:21

Woot, I tried Mr.Max's code AGAIN and this time it worked. Go Figure!

<SCRIPT TYPE="text/javascript" LANGUAGE="JavaScript">
<!-- ;

// Written by mr.maX, http://www.max.co.yu/

function getPageOffsetLeft() {
return arguments[0].offsetLeft + (arguments[0].offsetParent ? getPageOffsetLeft(arguments[0].offsetParent) : 0);
}

function getPageOffsetTop() {
return arguments[0].offsetTop + (arguments[0].offsetParent ? getPageOffsetTop(arguments[0].offsetParent) : 0);
}

function blah() {
theImage = document.getElementById("testImg2");
x = getPageOffsetLeft(theImage);
y = getPageOffsetTop(theImage);
alert(x+" "+y);
}

// -->
</SCRIPT>


Thanks,

Carnage



[This message has been edited by Carnage (edited 06-10-2001).]

« BackwardsOnwards »

Show Forum Drop Down Menu