Created
May 15, 2014 20:10
-
-
Save catdad/d8602f765056a31ea8da to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/** | |
* support for `Element.getBoudingClientRect()` goes back to IE5, FF3, and Chrome 1 | |
* it's great for getting the placement of a DOM element | |
* it is supposed to include `top`, `bottom`, `left`, and `right` in browser coordinates | |
* newer browsers also return `width` and `height` | |
* this code patches that, allowing all browsers access to the `widht` and `height` | |
*/ | |
function standardizeBoundingBox(bb) { | |
//IE8 does not have width and height in bounding boxes | |
return { | |
width: bb.width || (bb.right - bb.left), | |
height: bb.height || (bb.bottom - bb.top), | |
top: bb.top, | |
bottom: bb.bottom, | |
left: bb.left, | |
right: bb.right | |
}; | |
} | |
//usage | |
var el = document.getElementById('myElement'); | |
var boundingBox = standardizeBoundingBox( el.getBoundingClientRect() ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment