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
function snapshot(node) {
var computedStyle = {},
style = {},
prop;
if (typeof document.defaultView.getComputedStyle === "function") {
computedStyle = document.defaultView.getComputedStyle(node, null);
} else if (node.currentStyle) {
computedStyle = node.currentStyle;
} else {
throw new Error("Unable to get a computed style.");
}
for (prop in computedStyle) {
if (computedStyle.hasOwnProperty(prop)) {
style[prop] = computedStyle[prop];
}
}
return {
node: node.cloneNode(true),
style: style
}
}
// Whatever jquery supports
$.fn.snapshot = function () {
var node = this.get(0),
computedStyle = {},
style = {},
prop;
if (typeof document.defaultView.getComputedStyle === "function") {
computedStyle = document.defaultView.getComputedStyle(node, null);
} else if (node.currentStyle) {
computedStyle = node.currentStyle;
} else {
throw new Error("Unable to get a computed style.");
}
for (prop in computedStyle) {
if (computedStyle.hasOwnProperty(prop)) {
style[prop] = this.css(prop);
}
}
return {
node: this.clone(true),
style: style
}
}
var snap1 = snapshot(document.getElementById("target")),
snap2 = $("#target").snapshot();
console.log(snap1, snap2);