getBoundingClientRect and getBoxObjectFor

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

    #1

    getBoundingClientRect and getBoxObjectFor

    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.getBox ObjectFor(docum ent.getElementB yId("vert_px")) .y;

    This works on IE only on a PC:
    document.getEle mentById("vert_ px").getBoundin gClientRect().b ottom;

    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!

  • Martin Honnen

    #2
    Re: getBoundingClie ntRect and getBoxObjectFor



    parksch2@hotmai l.com wrote:
    [color=blue]
    > 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.getBox ObjectFor(docum ent.getElementB yId("vert_px")) .y;
    >
    > This works on IE only on a PC:
    > document.getEle mentById("vert_ px").getBoundin gClientRect().b ottom;
    >
    > Can anyone offer any advice on how I can get the position of this pixel
    > in Safari and IE on a Mac?[/color]

    Elements have properties
    offsetLeft
    offsetTop
    offsetParent
    offsetWidth
    offsetHeight
    so you can do
    function getCoords (element) {
    var coords = { x: 0, y: 0, width: element.offsetW idth, height:
    element.offsetH eight };
    while (element) {
    coords.x += element.offsetL eft;
    coords.y += element.offsetT op;
    element = element.offsetP arent;
    }
    return coords;
    }

    var coords = getCoords(docum ent.getElementB yId('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/

    Comment

    • parksch2@hotmail.com

      #3
      Re: getBoundingClie ntRect and getBoxObjectFor

      Martin, that works perfectly! Thanks a ton!

      Comment

      • RobG

        #4
        Re: getBoundingClie ntRect and getBoxObjectFor

        parksch2@hotmai l.com wrote:[color=blue]
        > Martin, that works perfectly! Thanks a ton!
        >[/color]

        For general interest, have a look at:

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

        Follow the viewport link.

        --
        Rob

        Comment

        Working...