Getting DOM element position(style.left and style.top)

Discussion in 'Javascript' started by Amir Hardon, May 31, 2004.

  1. Amir Hardon

    Amir Hardon Guest

    I'm sorry for the RTFM question, but these keywords are too
    common for being usefull with google.

    I'm trying to position a dynamically created DOM element
    (with style.position=absolute), in a position relative to
    another element which don't have an absolute position,
    My problem is getting the "absolute" position of that element.

    I wrote two function(below) to solve this problem,
    but I'm pretty sure there is a more effective solution:

    function getoffsetLeft(element){/* Calculate the offsetLeft sum of all
    offsetParents.
    The result is element.style.left
    */
    if(!element) return 0;
    return element.offsetLeft + getoffsetLeft(element.offsetParent);
    }

    function getoffsetTop(element){/* Calculate the offsetTop sum of all
    offsetParents.
    The result is element.style.top
    */
    if(!element) return 0;
    return element.offsetTop + getoffsetTop(element.offsetParent);
    }
     
    Amir Hardon, May 31, 2004
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Varangian

    Get the LEFT and TOP position of Image

    Varangian, Mar 30, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    501
    Varangian
    Mar 30, 2006
  2. Konrad Hammerer
    Replies:
    14
    Views:
    628
    Jonathan N. Little
    Feb 7, 2008
  3. Pieter Van Waeyenberge
    Replies:
    1
    Views:
    195
    Pieter Van Waeyenberge
    Feb 9, 2004
  4. Pugi!
    Replies:
    10
    Views:
    217
    Randy Webb
    Dec 19, 2006
  5. Stevo
    Replies:
    10
    Views:
    310
    Thomas 'PointedEars' Lahn
    Mar 27, 2008
Loading...

Share This Page