Re: [cssom-view] Why is body's offsetParent null ?

On Wed, 23 Apr 2008 11:10:34 +0200, Daniel Glazman  
<daniel.glazman@disruptive-innovations.com> wrote:
> Anne van Kesteren wrote:
>
>> Actually, you would probably always use getBoundingClientRect() as  
>> that's far more consistent and reliable than any of the offset*  
>> attributes. They are mainly defined because lots of legacy scripts use  
>> them currently to calculate the position of an element using while  
>> loops (which the specification handles fine).
>
> So the behavioural difference between body and other elements is
> introduced because of MSIE but the Web is full of the following
> script:

I've also seen scripts like

   while (ele != document.body) {
     t += ele.offsetTop
     ele = ele.offsetParent
   }


> var t = 0;
> var p = myElement;
> while (p) {
>    t += p.offsetTop;
>    p =  p.offsetParent;
> }
> // use t
>
> Because of the change you introduce saying document.body.offsetTop
> is now always zero, such scripts will give an erroneous result for
> non-IE browsers. Right ?

No, the way it works is that if offsetParent is equal to the HTML body  
element you give the dinstance relative to the initial containing block  
origin rather than the HTML body element. This is how non-IE browsers  
already function. (That's step 2 in the offsetTop algorithm.)


-- 
Anne van Kesteren
<http://annevankesteren.nl/>
<http://www.opera.com/>

Received on Wednesday, 23 April 2008 09:40:41 UTC