Skip to content

Instantly share code, notes, and snippets.

@jcubic
Created January 24, 2018 09:13
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 jcubic/9ef9fa2561de8430e953e2fe62011c20 to your computer and use it in GitHub Desktop.
Save jcubic/9ef9fa2561de8430e953e2fe62011c20 to your computer and use it in GitHub Desktop.
Monkey patch for jQuery css to handle custom properties aka css variables
$.fn.css = (function(css) {
return function fn(key, value) {
var self = this;
if (arguments.length == 1 && $.isPlainObject(arguments[0])) {
var data = arguments[0];
Object.keys(data).forEach(function(key) {
fn.call(self, key, data[key]);
});
} else if (key.match(/^\s*--/)) {
if (typeof value === 'undefined') {
return self[0].style.getPropertyValue(key);
} else {
self[0].style.setProperty(key, value);
}
} else {
return css.apply(self, arguments);
}
return self;
};
})($.fn.css);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment