element position inside scrollable div tag

Discussion in 'Javascript' started by unixfreak compiler, Feb 17, 2006.

  1. I'm using the algorithm listed beow to get the position of an element.
    Some elements are located inside DIV tags that can scroll. The
    scrolling doesn't seem to change the X and Y coordinates I get back
    from this algorithm so it ends up being wrong.

    What do I need to do to include the effects of the scroll bar?

    - john

    function getElementPosition(elementID) {

    var element = document.getElementById(elementID);
    if (element == null) { return null; }
    var xPos = findPosX(element);
    var yPos = findPosY(element);

    return { x : xPos, y : yPos };
    }

    function findPosX(obj) {

    var curleft = 0;

    if (obj.offsetParent) {
    while (obj.offsetParent) {
    curleft += obj.offsetLeft
    obj = obj.offsetParent;
    }
    } else if (obj.x) {
    curleft += obj.x;
    }

    return curleft;
    }

    function findPosY(obj) {

    var curtop = 0;

    if (obj.offsetParent) {
    while (obj.offsetParent) {
    curtop += obj.offsetTop
    obj = obj.offsetParent;
    }
    } else if (obj.y) {
    curtop += obj.y;
    }

    return curtop;
    }
     
    unixfreak compiler, Feb 17, 2006
    #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. Marcus
    Replies:
    0
    Views:
    501
    Marcus
    Oct 6, 2008
  2. Muralidhar

    Setting Scrollable position for div tag

    Muralidhar, Sep 1, 2003, in forum: Javascript
    Replies:
    0
    Views:
    166
    Muralidhar
    Sep 1, 2003
  3. M Wells
    Replies:
    0
    Views:
    146
    M Wells
    Oct 6, 2004
  4. visu
    Replies:
    4
    Views:
    321
  5. Replies:
    4
    Views:
    102
    Peter Michaux
    Jan 6, 2007
Loading...

Share This Page