Skip to content

Instantly share code, notes, and snippets.

@mrmartineau
Created August 15, 2013 00:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrmartineau/6237080 to your computer and use it in GitHub Desktop.
Save mrmartineau/6237080 to your computer and use it in GitHub Desktop.
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