JSFiddle - React, Tailwind, and code Playground
by Graham Fairweather
HTML
<div id="target">Text</div>
CSS
#target {
position: relative;
top: 100px;
left: 150px;
height: 50px;
width: 70px;
}
JavaScript
// IE5+ support
var getBoundingClientRect = (function () {
// Gecko < 1.9.1 test
var test = document.body.getBoundingClientRect();
// Gecko < 1.9.1
if (!test.hasOwnProperty("height") || !test.hasOwnProperty("width")) {
return function (node) {
var rect = target.getBoundingClientRect();
rect.height = rect.bottom - rect.top;
rect.width = rect.right - rect.left;
return rect;
};
}
// Gecko >= 1.9.1
return function (node) {
return target.getBoundingClientRect();
};
}());
// Whatever jquery supports
$.fn.getBoundingClientRect = function () {
var offset = this.offset(),
height = this.height(),
width =this.width(),
rect = {
width: width,
height: height,
top: offset.top,
left: offset.left,
bottom: offset.top + height,
right: offset.left + width
};
return rect;
}
var rect1 = getBoundingClientRect(document.getElementById("target")),
rect2 = $("#target").getBoundingClientRect();
console.log(rect1, rect2);