PDA

View Full Version : offsetleft gives you dist from parent, but what if you want relative to whole page?


x_goose_x
08-09-2002, 07:30 AM
Pretty much what’s in the title. element.offsetLeft tells you the horizontal distance of an element relative to it's parent element, but once there is a hierarchy of children, it gets annoying adding up all the offsets. How do you determine the offset directly, without going through the parent elements. Thanx.

-x_goose_x

beetle
08-09-2002, 07:45 AM
Dont' know. but it shouldn't be that hard to code up a function that will get the offset of any object passed to it....

Roy Sinclair
08-09-2002, 02:34 PM
Try looking at left and pixelLeft.

brothercake
08-09-2002, 02:46 PM
What you need is a recursive function; check out http://www.webreference.com/js/column33/image.html

x_goose_x
08-09-2002, 03:50 PM
Thats what I figured. Just wondering if there was a direct weay of doing it. Thanks brothercake.

jkd
08-09-2002, 06:12 PM
Gecko:

document.getBoxObjectFor(element).x

is the offset from the left edge from the page. screenX is from the screen.

For compatibility, I generally use:

function getLeft(node) {
var temp = node;
var left = parseInt(node.offsetLeft);
while (temp = temp.parentNode && temp.offsetLeft)
left += parseInt(temp.offsetLeft);
return left;
}

Or something like that (not one I pulled off my hdd, just something I typed here, may or may not be entirely correct).