W3C home > Mailing lists > Public > www-style@w3.org > April 2008

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

From: Anne van Kesteren <annevk@opera.com>
Date: Wed, 23 Apr 2008 11:27:13 +0200
To: "Daniel Glazman" <daniel.glazman@disruptive-innovations.com>, "www-style@w3.org" <www-style@w3.org>
Message-ID: <op.t914jntz64w2qv@annevk-t60.oslo.opera.com>

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 GMT

This archive was generated by hypermail 2.2.0+W3C-0.50 : Monday, 27 April 2009 13:55:05 GMT