468,741 Members | 1,581 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,741 developers. It's quick & easy.

Different offsetLeft/Top calculation for elements contained in DIV and TABLE/TD

Hello

In the code below, theres a DIV, a TABLE and another DIV , each with an
image in it.

When i read out the offsetLeft/Top from imgB in the table , i get offset
values relative to the containng TD ... *as it should be*, HOWEVER, the
offsetLeftTop values for imgA/C in the DIVs are calulated realtive to the
window border ???? is this normal

this happens on both NS7 & IE6

sample code (you can copy/paste ... i tested this):

<html>
<head>
<script language="JavaScript">
onload = function(){
var imgA = document.getElementById('imgA');
var imgB = document.getElementById('imgB');
var imgC = document.getElementById('imgC');

alert(imgA.offsetLeft)
alert(imgA.offsetTop)

alert(imgB.offsetLeft)
alert(imgB.offsetTop)

alert(imgC.offsetLeft)
alert(imgC.offsetTop)

}

</script>
</head>

<body>

<div>
<img src="0.jpg" id="imgA" width="100" height="100">
</div>

<table>
<tr>
<td>
<img src="1.jpg" id="imgB" width="100" height="100">
</td>
</tr>
</table>

<div>
<img src="2.jpg" id="imgC" width="100" height="100">
</div>
</body>
</html>

Jul 20 '05 #1
2 2216


Pieter Van Waeyenberge wrote:
In the code below, theres a DIV, a TABLE and another DIV , each with an
image in it.

When i read out the offsetLeft/Top from imgB in the table , i get offset
values relative to the containng TD ... *as it should be*, HOWEVER, the
offsetLeftTop values for imgA/C in the DIVs are calulated realtive to the
window border ???? is this normal


offsetLeft/offsetTop are relative to the offsetParent which can be
different from parentNode.

--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 20 '05 #2
thnx!

"Martin Honnen" <ma*******@yahoo.de> wrote in message
news:40******@olaf.komtel.net...


Pieter Van Waeyenberge wrote:
In the code below, theres a DIV, a TABLE and another DIV , each with an image in it.

When i read out the offsetLeft/Top from imgB in the table , i get offset values relative to the containng TD ... *as it should be*, HOWEVER, the
offsetLeftTop values for imgA/C in the DIVs are calulated realtive to the window border ???? is this normal


offsetLeft/offsetTop are relative to the offsetParent which can be
different from parentNode.

--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Keith Thornhill | last post: by
3 posts views Thread by ara.pehlivanian | last post: by
2 posts views Thread by Gary Coutts | last post: by
11 posts views Thread by David Stone | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.