Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #13207

Re: Finding the V offset of an element

From "Leonardo Azpurua" <leonardo@exmvps.org>
Newsgroups comp.lang.javascript
Subject Re: Finding the V offset of an element
Date 2012-04-03 22:18 -0430
Organization A noiseless patient Spider
Message-ID <jlgcp0$kss$1@dont-email.me> (permalink)
References <jlgbcm$eti$1@dont-email.me>

Show all headers | View raw


"Leonardo Azpurua" <leonardo@exmvps.org> escribió en el mensaje 
news:jlgbcm$eti$1@dont-email.me...
> Hi,
>
> I have to place a DIV containing a calendar on top of the input element 
> where it will store the picked date.
>
> This input element may be contained within another element (ok, I use HTML 
> tables to organize labels and input controls), which may in turn be 
> contained within another.
>
> In Opera and FF, the containing hierarchy (inverted) is this:
>
>    InputElement
>        TableCellElement
>            TableRowElement
>                TableSectionElement
>                    TableElement
>                        FormElement
>
> I wrote this (very naive) function:
>
> function fullOffsetTop(elt)
> {
>    n = 0;
>    while (elt && elt.offsetTop)
>    {
>        n += elt.offsetTop;
>        elt = elt.parentNode;
>    }
>    return n;
> }
>
> Which has two (if not more) problems: first, both TableCellElement and 
> TableRowElement have the same offset; and also, web-kit based browsers 
> (Chrome and Safari) either have a different hierarchy or else offsetTop 
> becomes undefined earlier.
>
> The question is whether there is a safe (and clean, if possible) method to 
> find out the absolute position of an element, no matter how deeply nested 
> it is.

Fiddling more with the problem, I rewrote the fullOffsetTop function thus:

function fullOffsetTop(elt)
{
 n = 0;
 while (elt)
 {
window.alert(elt.toString() + "\noffsetTop = " + elt.offsetTop);
  s = elt.toString();
  if (elt.offsetTop) n += elt.offsetTop;
  elt = elt.parentNode;
 }
 return n;
}

And found that web-kit based browser have exactly the same hierarchy, but it 
was stopping because some intermediate element hadn't the offsetTop property 
defined.

Then I noticed that the Form element had the same offset as the Table 
element, and so did TableRow and TableSection. So I removed them from the 
accumulated total, thus:

function fullOffsetTop(elt)
{
 n = 0;
 while (elt)
 {
  s = elt.toString();
  if (elt.offsetTop &&
    s.indexOf("TableRow") < 0 &&
    s.indexOf("Form") < 0) n += elt.offsetTop;
  elt = elt.parentNode;
 }
 return n;
}

Now I seem to be getting (kind of) the intended result.

Nevertheless, it feels messy.

If somebody could point me to a better way of properly placing this element, 
I would greatly appreciate it.

TIA,

Leonardo
--

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Finding the V offset of an element "Leonardo Azpurua" <leonardo@exmvps.org> - 2012-04-03 21:54 -0430
  Re: Finding the V offset of an element "Leonardo Azpurua" <leonardo@exmvps.org> - 2012-04-03 22:18 -0430
    Re: Finding the V offset of an element "Leonardo Azpurua" <leonardo@exmvps.org> - 2012-04-03 22:34 -0430
    Re: Finding the V offset of an element Hans-Georg Michna <hans-georgNoEmailPlease@michna.com> - 2012-04-04 14:49 +0200
      Re: Finding the V offset of an element "Leonardo Azpurua" <leonardo@exmvps.org> - 2012-04-04 10:28 -0430
        Re: Finding the V offset of an element Hans-Georg Michna <hans-georgNoEmailPlease@michna.com> - 2012-04-05 09:12 +0200
      Re: Finding the V offset of an element Eric Bednarz <bednarz@fahr-zur-hoelle.org> - 2012-04-04 20:28 +0200
        Re: Finding the V offset of an element Hans-Georg Michna <hans-georgNoEmailPlease@michna.com> - 2012-04-05 09:13 +0200
      Re: Finding the V offset of an element Richard Cornford <Richard@litotes.demon.co.uk> - 2012-04-04 07:05 -0700
        Re: Finding the V offset of an element Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-04-04 22:28 +0200
          Re: Finding the V offset of an element Eric Bednarz <bednarz@fahr-zur-hoelle.org> - 2012-04-05 00:44 +0200
            Re: Finding the V offset of an element Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-04-05 05:50 +0200
              Re: Finding the V offset of an element Eric Bednarz <bednarz@fahr-zur-hoelle.org> - 2012-04-05 11:22 +0200
                Re: Finding the V offset of an element Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-04-05 13:21 +0200
          Re: Finding the V offset of an element Richard Cornford <Richard@litotes.demon.co.uk> - 2012-04-05 04:35 -0700
            Re: Finding the V offset of an element Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-04-05 14:02 +0200
              Re: Finding the V offset of an element Richard Cornford <Richard@litotes.demon.co.uk> - 2012-04-10 09:32 -0700
          Re: Finding the V offset of an element John G Harris <john@nospam.demon.co.uk> - 2012-04-05 14:54 +0100
            Re: Finding the V offset of an element Gene Wirchenko <genew@ocis.net> - 2012-04-05 08:37 -0700
  Re: Finding the V offset of an element Richard Cornford <Richard@litotes.demon.co.uk> - 2012-04-04 04:57 -0700
    Re: Finding the V offset of an element Hans-Georg Michna <hans-georgNoEmailPlease@michna.com> - 2012-04-05 09:16 +0200
      Re: Finding the V offset of an element "Richard Cornford" <Richard@litotes.demon.co.uk> - 2012-04-05 09:38 +0100
        Re: Finding the V offset of an element Hans-Georg Michna <hans-georgNoEmailPlease@michna.com> - 2012-04-08 18:27 +0200
  Re: Finding the V offset of an element Dr J R Stockton <reply1214@merlyn.demon.co.uk.not.invalid> - 2012-04-06 21:52 +0100
    Re: Finding the V offset of an element "Leonardo Azpurua" <leonardo@exmvps.org> - 2012-04-07 11:18 -0430

csiph-web