Getting DOM element position(style.left and style.top)

A

Amir Hardon

I'm sorry for the RTFM question, but these keywords are too
common for being usefull with google.

I'm trying to position a dynamically created DOM element
(with style.position=absolute), in a position relative to
another element which don't have an absolute position,
My problem is getting the "absolute" position of that element.

I wrote two function(below) to solve this problem,
but I'm pretty sure there is a more effective solution:

function getoffsetLeft(element){/* Calculate the offsetLeft sum of all
offsetParents.
The result is element.style.left
*/
if(!element) return 0;
return element.offsetLeft + getoffsetLeft(element.offsetParent);
}

function getoffsetTop(element){/* Calculate the offsetTop sum of all
offsetParents.
The result is element.style.top
*/
if(!element) return 0;
return element.offsetTop + getoffsetTop(element.offsetParent);
}
 

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,731
Messages
2,569,432
Members
44,835
Latest member
KetoRushACVBuy

Latest Threads

Top