Skip to content

Instantly share code, notes, and snippets.

@antfu
Created November 20, 2016 16:02
Show Gist options
  • Save antfu/28eb5d5ad22888cd4800bb94cfca8a38 to your computer and use it in GitHub Desktop.
Save antfu/28eb5d5ad22888cd4800bb94cfca8a38 to your computer and use it in GitHub Desktop.
[JS] Getting All CSS Properties of a DOM
function get_style( dom ) {
var style;
var returns = {};
// FireFox and Chrome way
if(window.getComputedStyle){
style = window.getComputedStyle(dom, null);
for(var i = 0, l = style.length; i < l; i++){
var prop = style[i];
var val = style.getPropertyValue(prop);
returns[prop] = val;
}
return returns;
}
// IE and Opera way
if(dom.currentStyle){
style = dom.currentStyle;
for(var prop in style){
returns[prop] = style[prop];
}
return returns;
}
// Style from style attribute
if(style = dom.style){
for(var prop in style){
if(typeof style[prop] != 'function'){
returns[prop] = style[prop];
}
}
return returns;
}
return returns;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment