Skip to content

Instantly share code, notes, and snippets.

@loktar00
Created September 27, 2012 15:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save loktar00/3794579 to your computer and use it in GitHub Desktop.
Save loktar00/3794579 to your computer and use it in GitHub Desktop.
Get dimensions and position of element
function getOffsetRect(elem) {
var box = elem.getBoundingClientRect(),
body = document.body,
docElem = document.documentElement,
scrollTop = window.pageYOffset || docElem.scrollTop || body.scrollTop,
scrollLeft = window.pageXOffset || docElem.scrollLeft || body.scrollLeft,
clientTop = docElem.clientTop || body.clientTop || 0,
clientLeft = docElem.clientLeft || body.clientLeft || 0,
top = box.top + scrollTop - clientTop,
left = box.left + scrollLeft - clientLeft,
right = box.right + scrollLeft - clientLeft,
bottom = box.bottom + scrollTop - clientTop;
return {
x: Math.round(left),
y: Math.round(top),
width: Math.round(right - left),
height: Math.round(bottom - top)
};
}​
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment