Created
November 20, 2016 16:02
-
-
Save antfu/28eb5d5ad22888cd4800bb94cfca8a38 to your computer and use it in GitHub Desktop.
[JS] Getting All CSS Properties of a DOM
This file contains 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
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