Skip to content

Instantly share code, notes, and snippets.

@catdad
Created May 15, 2014 20:10
Show Gist options
  • Save catdad/d8602f765056a31ea8da to your computer and use it in GitHub Desktop.
Save catdad/d8602f765056a31ea8da to your computer and use it in GitHub Desktop.
/**
* 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