Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple shim to make getting and setting CSS values more easy
// Get/Set CSS styles with ease
function styler(el) {
return {
/* Get CSS style
* @prop : String - CSS property name e.g 'width', 'height'
* @int : Boolean
*/
get : function(prop, int) {
/* TODO:
* Get multiple CSS properties. prop could be comma separated list
* Allow get all css values
* Throw errors when args not in correct format
*/
var int = int || false;
if (int === true) {
return parseInt(window.getComputedStyle(el, null).getPropertyValue(prop), 10);
} else{
return window.getComputedStyle(el, null).getPropertyValue(prop);
}
},
/* Set CSS style
* @prop : String - CSS property name e.g 'width', 'height'
* @val : String - CSS property value e.g. '200px'
*/
set : function(prop, val) {
/* TODO:
* Set multiple CSS properties. prop could be comma separated list
* Throw errors when args not in correct format
*/
return el.style[prop] = val;
}
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment