element position in nested tables??!!

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • travis_brooks@hotmail.com

    element position in nested tables??!!

    greetings

    I had a javascript that was reliably telling me the position of an
    image on a page, and now its not. I was using the script to draw a
    bounding box that followed the mouse around over the image, and when
    you clicked the mouse the x-y position of the image pixel the mouse was
    over was posted back in the form. I just changed the layout of the
    page so that the image is now in a table that is nested in another
    table, and the old reliable script is now giving me the position of the
    image as the upper left corner of the outer table a couple hundred
    pixels in the wrong direction. When i first wrote the script i googled
    around and people were saying you had to traverse up the document tree
    to find the top (document level) offsetParent element to determine the
    true offsetLeft and offsetTop of an element, which is what i did, and
    which is what seemed to work for a while. BUT, once i nested these
    tables that doesn't seem to work. Is this a common problem, or have i
    just screwed up somewhere??

    Here's the script i was using to find the position:

    ////

    function Point(xpos, ypos){
    this.x=xpos;
    this.y=ypos;
    }

    function findPosition(el ement){
    var currPt = new Point(0, 0);
    if(element.offs etParent){
    // just march up the document tree
    while(element.o ffsetParent){
    currPt.x = element.offsetL eft;
    currPt.y = element.offsetT op;
    element = element.offsetP arent;
    }
    }else{
    if(element.x)
    currPt.x = element.x;
    if(element.y)
    currPt.y = element.y;
    }
    return currPt;
    }

    ////

    -thanks

    -travis

  • Martin Honnen

    #2
    Re: element position in nested tables??!!



    travis_brooks@h otmail.com wrote:

    [color=blue]
    > function findPosition(el ement){
    > var currPt = new Point(0, 0);
    > if(element.offs etParent){
    > // just march up the document tree
    > while(element.o ffsetParent){[/color]

    Try
    while (element) {
    [color=blue]
    > currPt.x = element.offsetL eft;
    > currPt.y = element.offsetT op;
    > element = element.offsetP arent;
    > }[/color]

    otherwise your code misses out on the last element in the offset
    hierarchy I think.

    --

    Martin Honnen

    Comment

    Working...