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;
}
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;
}