getBoundingClientRect and getBoxObjectFor

P

parksch2

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!
 
M

Martin Honnen

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top