PDA

View Full Version : Absolute position of relatively positioned <div>


Evlich
01-26-2003, 11:40 PM
I need to create a clip taht is exactly over a div, but the div is located within other elements so when i use offsetTop, I get something like 2, when the div is on the right side of the screen. Is there a way that i can get the top and left relative to the WHOLE document rather than just the div above it? Thanks a lot.
~evlich

jkd
01-27-2003, 12:29 AM
function getLeft(element) {
return parseInt(element.offsetLeft) + ((element.parentNode && element.parentNode.offsetLeft) ? getLeft(element.parentNode) : 0);
}

Evlich
01-27-2003, 02:52 AM
It didn't seem to work. Here is what I have:
<div style="position: absolute; top: 100px;">
<div>
<div id="position">
hello
</div>
</div>
</div>
And when I run the statement:
function getAbsTop(element)
{
return parseInt(element.offsetTop) + ((element.parentNode && element.parentNode.offsetTop) ? getAbsTop(element.parentNode) : 0);
}
with the code:
alert(getAbsTop(getElement('position')));
and I get 0 in my box and clearly the position is 100. Any idea as to what I might have done?
Thanks a lot.
~evlich

MrDoubtFire
01-27-2003, 03:47 AM
You sure "position" isn't a reserved word?

Evlich
01-27-2003, 04:32 AM
I think that it is because element.parentNode.offsetTop is equal to 0, i think that i need to replace that part of the statement with something like "is a valid command." Is there anything that I can use for this?
~evlich