That's actually not as effective though. If you have nested elements the offsetParent may be one of the element's more immediate containing elements, if so - the math and/or the actually positioning (visa vis appearing fixed) may not work out as well.
With certain types of nesting even going directly for the body/documentElement will not work out the same as position:fixed; does in the browsers that support it. But it will be more accurate than the offsetParent method you've just outlined, which can fail to fix the element at all, while the body/documentElement method will at least still fix the element, just not in the location that corresponds to where it would be in a browser supporting fixed positioning. Adjustments can be made (interestingly enough, also involving the offsetParent would be one way) so that it does, not so with offsetParent alone.
Consider trying to mimic the fixed positioning here:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
.X {
position:fixed;
top:0;
left:100px;
}
</style>
</head>
<body>
<div>
More
</div>
<div>
Lo
<div style="position:relative;">
<table>
<tr>
<td><div class="X">
Hi
</div></td>
</tr>
</table>
</div>
</div>
<p>something</p>
<p>something</p>
<p>something</p>
<p>something</p>
<p>something</p>
<p>something</p>
<p>something</p>
<p>something</p>
<p>something</p>
<p>something</p>
<p>something</p>
<p>something</p>
<p>something</p>
<p>something</p>
<p>something</p>
<p>something</p>
<p>something</p>
<p>something</p>
<p>something</p>
<p>something</p>
<p>something</p>
<p>something</p>
<p>something</p>
<p>something</p>
<p>something</p>
<p>something</p>
<p>something</p>
<p>something</p>
<p>something</p>
<p>something</p>
<p>something</p>
<p>something</p>
<p>something</p>
<p>something</p>
<p>something</p>
<p>something</p>
<p>something</p>
<p>something</p>
</body>
</html>
Bookmarks