Created
September 27, 2012 15:19
-
-
Save loktar00/3794579 to your computer and use it in GitHub Desktop.
Get dimensions and position of element
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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