brgivens
asked on
offsetLeft problem
Hi,
I'm having a problem calculating the actual position of an element within the body:
<script>
function test(){
document.getElementById("b lock").sty le.left = offsetLeft(document.getEle mentById(" cell1"));
document.getElementById("b lock").sty le.top = offsetTop(document.getElem entById("c ell1"));
alert("Table offsetLeft = " + document.getElementById("t able1").of fsetLeft + "px");
}
function offsetLeft(o){var i=0;while(o.offsetParent!= null){i+=o .offsetLef t;o=o.offs etParent;} return i+o.offsetLeft;}
function offsetTop(o){var i=0;while(o.offsetParent!= null){i+=o .offsetTop ;o=o.offse tParent;}r eturn i+o.offsetTop;}
</script>
<body onload="test();">
<div style="position:relative">
<table id=table1 cellspacing=0 cellpadding=0>
<tr>
<td id=cell1 style="border:1px solid black"><input></td>
</tr>
</table>
</div>
<div id=block style="background:black;po sition:abs olute;heig ht:200;wid th:2" />
</body>
The black line should line up with the left side of the cell. It works fine in NN but not in IE. For some reason, IE thinks the table is offset 10px from div. I find it interesting that the same logic does work to determine the top position of the cell.
The problem does not occur if the div isn't relative, but this example is reduced from the original to illustrate the actual problem; the div must remain relative.
Can anyone figure out how to fix my offsetLeft function so that it returns the correct total offsetLeft value in IE?
I'm having a problem calculating the actual position of an element within the body:
<script>
function test(){
document.getElementById("b
document.getElementById("b
alert("Table offsetLeft = " + document.getElementById("t
}
function offsetLeft(o){var i=0;while(o.offsetParent!=
function offsetTop(o){var i=0;while(o.offsetParent!=
</script>
<body onload="test();">
<div style="position:relative">
<table id=table1 cellspacing=0 cellpadding=0>
<tr>
<td id=cell1 style="border:1px solid black"><input></td>
</tr>
</table>
</div>
<div id=block style="background:black;po
</body>
The black line should line up with the left side of the cell. It works fine in NN but not in IE. For some reason, IE thinks the table is offset 10px from div. I find it interesting that the same logic does work to determine the top position of the cell.
The problem does not occur if the div isn't relative, but this example is reduced from the original to illustrate the actual problem; the div must remain relative.
Can anyone figure out how to fix my offsetLeft function so that it returns the correct total offsetLeft value in IE?
You should give your body a margin:
<script>
function test(){
//NOTE THAT I SUBTRACTED THE BODY MARGIN BELOW
document.getElementById("b lock").sty le.left = offsetLeft(document.getEle mentById(" cell1"))-p arseInt(do cument.bod y.style.ma rgin);
document.getElementById("b lock").sty le.top = offsetTop(document.getElem entById("c ell1"));
alert("Table offsetLeft = " + document.getElementById("t able1").of fsetLeft + "px");
}
function offsetLeft(o){var i=0;while(o.offsetParent!= null){i+=o .offsetLef t;o=o.offs etParent;} return i+o.offsetLeft;}
function offsetTop(o){var i=0;while(o.offsetParent!= null){i+=o .offsetTop ;o=o.offse tParent;}r eturn i+o.offsetTop;}
</script>
<body onload="test();" style="margin:10px;">
<div style="position:relative">
<table id=table1 cellspacing=0 cellpadding=0>
<tr>
<td id=cell1 style="border:1px solid black"><input></td>
</tr>
</table>
</div>
<div id=block style="background:black;po sition:abs olute;heig ht:200;wid th:2" />
</body>
<script>
function test(){
//NOTE THAT I SUBTRACTED THE BODY MARGIN BELOW
document.getElementById("b
document.getElementById("b
alert("Table offsetLeft = " + document.getElementById("t
}
function offsetLeft(o){var i=0;while(o.offsetParent!=
function offsetTop(o){var i=0;while(o.offsetParent!=
</script>
<body onload="test();" style="margin:10px;">
<div style="position:relative">
<table id=table1 cellspacing=0 cellpadding=0>
<tr>
<td id=cell1 style="border:1px solid black"><input></td>
</tr>
</table>
</div>
<div id=block style="background:black;po
</body>
But then it doesn't work in Netscape, better do this:
function test(){
document.getElementById("b lock").sty le.left = offsetLeft(document.getEle mentById(" cell1"))-( document.a ll?parseIn t(document .body.styl e.margin): 0);
document.getElementById("b lock").sty le.top = offsetTop(document.getElem entById("c ell1"));
alert("Table offsetLeft = " + document.getElementById("t able1").of fsetLeft + "px");
}
function test(){
document.getElementById("b
document.getElementById("b
alert("Table offsetLeft = " + document.getElementById("t
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
brgivens, thanks for the points, but is there a reason for the B?
If you were looking for an explanation, it is because in IE, with relatively positioned divs, the body margin is not taken into account. This is why I check how big the body margin is, and then add this amount to the other number.
Cd&