getBoundingClientRect and getBoxObjectFor

Discussion in 'Javascript' started by parksch2@hotmail.com, Apr 19, 2005.

  1. Guest

    I'm having trouble finding the position of a pixel on a page on Safari
    and IE on a Mac.

    This works in Firefox and Netscape on PC and Mac:
    document.getBoxObjectFor(document.getElementById("vert_px")).y;

    This works on IE only on a PC:
    document.getElementById("vert_px").getBoundingClientRect().bottom;

    Can anyone offer any advice on how I can get the position of this pixel
    in Safari and IE on a Mac?

    Thanks so much in advance!
     
    , Apr 19, 2005
    #1
    1. Advertising

  2. wrote:

    > I'm having trouble finding the position of a pixel on a page on Safari
    > and IE on a Mac.
    >
    > This works in Firefox and Netscape on PC and Mac:
    > document.getBoxObjectFor(document.getElementById("vert_px")).y;
    >
    > This works on IE only on a PC:
    > document.getElementById("vert_px").getBoundingClientRect().bottom;
    >
    > Can anyone offer any advice on how I can get the position of this pixel
    > in Safari and IE on a Mac?


    Elements have properties
    offsetLeft
    offsetTop
    offsetParent
    offsetWidth
    offsetHeight
    so you can do
    function getCoords (element) {
    var coords = { x: 0, y: 0, width: element.offsetWidth, height:
    element.offsetHeight };
    while (element) {
    coords.x += element.offsetLeft;
    coords.y += element.offsetTop;
    element = element.offsetParent;
    }
    return coords;
    }

    var coords = getCoords(document.getElementById('elementId'));
    alert(coords.x + ':' + coords.y);

    Note that I think that Safari implements the offset* properties but I
    have not checked that code with Safari now.


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Apr 19, 2005
    #2
    1. Advertising

  3. Guest

    Martin, that works perfectly! Thanks a ton!
     
    , Apr 19, 2005
    #3
  4. RobG Guest

    wrote:
    > Martin, that works perfectly! Thanks a ton!
    >


    For general interest, have a look at:

    <URL:http://www.quirksmode.org/>

    Follow the viewport link.

    --
    Rob
     
    RobG, Apr 19, 2005
    #4
    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. Nut Cracker
    Replies:
    1
    Views:
    344
  2. jakk
    Replies:
    4
    Views:
    12,244
  3. Tim Meagher
    Replies:
    8
    Views:
    1,293
    Karl Seguin
    Jun 18, 2005
  4. titi

    if and and vs if and,and

    titi, Mar 9, 2007, in forum: VHDL
    Replies:
    4
    Views:
    593
    Mike Treseler
    Mar 11, 2007
  5. ameshkin
    Replies:
    15
    Views:
    219
    Thomas 'PointedEars' Lahn
    Nov 21, 2008
Loading...

Share This Page