Saturday, December 1, 2007

Find Position

Each html elements in a page has position values (ie. where the elements currently
placed). Basically it is measured by pixels from top and pixels from left.The top
and left position values are obtained by using the html elements attributs offsetTop
and offsetLeft.This can be calculated at run time using javascrit.
The script follows. Argument of the function is the object of the HTML element,
and it returns the array of values.

function findPos(obj) {
var curleft = curtop = 0;
if (obj.offsetParent) {
curleft = obj.offsetLeft
curtop = obj.offsetTop
while (obj = obj.offsetParent) {
curleft += obj.offsetLeft
curtop += obj.offsetTop
}
}
return [curleft,curtop];
}

No comments:

Post a Comment

Yahoo! News: Technology News