Skip to content

Instantly share code, notes, and snippets.

@jonathantneal
Created December 20, 2011 04:37
Show Gist options
  • Save jonathantneal/1500278 to your computer and use it in GitHub Desktop.
Save jonathantneal/1500278 to your computer and use it in GitHub Desktop.
getElementOffset.js
function getElementOffset(el)
{
for (var offsetTop = 0, offsetLeft = 0; el.offsetParent; el = el.parentNode)
{
offsetTop += el.offsetTop;
offsetLeft += el.offsetLeft;
}
return {
offsetTop: offsetTop,
offsetLeft: offsetLeft
};
}
// gf3 style
function getElementOffset(el) {
var offsetTop = 0, offsetLeft = 0;
if (el.offsetParent)
{
do {
offsetTop += el.offsetTop;
offsetLeft += el.offsetLeft;
} while (el = el.offsetParent);
}
return {
offsetTop: offsetTop,
offsetLeft: offsetLeft
};
}
@jonathantneal
Copy link
Author

Great ideas, @gf3, and I think it could be smaller too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment