Connecting Tech Pros Worldwide Forums | Help | Site Map

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

Pieter Van Waeyenberge
Guest
 
Posts: n/a
#1: Jul 20 '05
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>




Martin Honnen
Guest
 
Posts: n/a
#2: Jul 20 '05

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




Pieter Van Waeyenberge wrote:
[color=blue]
> 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[/color]

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

--

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

Pieter Van Waeyenberge
Guest
 
Posts: n/a
#3: Jul 20 '05

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


thnx!

"Martin Honnen" <mahotrash@yahoo.de> wrote in message
news:4030c952@olaf.komtel.net...[color=blue]
>
>
> Pieter Van Waeyenberge wrote:
>[color=green]
> > In the code below, theres a DIV, a TABLE and another DIV , each with[/color][/color]
an[color=blue][color=green]
> > image in it.
> >
> > When i read out the offsetLeft/Top from imgB in the table , i get[/color][/color]
offset[color=blue][color=green]
> > values relative to the containng TD ... *as it should be*, HOWEVER, the
> > offsetLeftTop values for imgA/C in the DIVs are calulated realtive to[/color][/color]
the[color=blue][color=green]
> > window border ???? is this normal[/color]
>
> offsetLeft/offsetTop are relative to the offsetParent which can be
> different from parentNode.
>
> --
>
> Martin Honnen
> http://JavaScript.FAQTs.com/
>[/color]


Closed Thread