Finding the offset of a page item

A couple of functions from my friend Jonathan Beckett for finding the x and y offsets of an item on a page. Basically, it works it’s way back up the DOM finding the offset of each parent item. There may be better ways; I haven’t checked that hard.
function get_x(obj){
var xpos = 0;
if (obj.offsetParent){
while (obj.offsetParent){
xpos += obj.offsetLeft;
obj = obj.offsetParent;
}
}
else if (obj.x) xpos += obj.x;
return xpos;
}

function get_y(obj){
var ypos = 0;
if (obj.offsetParent){
while (obj.offsetParent){
ypos += obj.offsetTop;
obj = obj.offsetParent;
}
}
else if (obj.y) ypos += obj.y;
return ypos;
}

Advertisements
Finding the offset of a page item

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s