element position inside scrollable div tag

U

unixfreak compiler

I'm using the algorithm listed beow to get the position of an element.
Some elements are located inside DIV tags that can scroll. The
scrolling doesn't seem to change the X and Y coordinates I get back
from this algorithm so it ends up being wrong.

What do I need to do to include the effects of the scroll bar?

- john

function getElementPosition(elementID) {

var element = document.getElementById(elementID);
if (element == null) { return null; }
var xPos = findPosX(element);
var yPos = findPosY(element);

return { x : xPos, y : yPos };
}

function findPosX(obj) {

var curleft = 0;

if (obj.offsetParent) {
while (obj.offsetParent) {
curleft += obj.offsetLeft
obj = obj.offsetParent;
}
} else if (obj.x) {
curleft += obj.x;
}

return curleft;
}

function findPosY(obj) {

var curtop = 0;

if (obj.offsetParent) {
while (obj.offsetParent) {
curtop += obj.offsetTop
obj = obj.offsetParent;
}
} else if (obj.y) {
curtop += obj.y;
}

return curtop;
}
 

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,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top