Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Edit Style Sheet Javascript
/*
var name = 'nav-bar';
var selector = '.nav-bar > ul.active';
var key = 'transform';
getRule(name, selector, key, function (sTransform) {
var values = getTranslateValues(sTransform);
var x = values[0] - 100;
var y = values[1];
var z = values[2];
setRule(name, selector, key, 'translate3d('+ x +'%,'+ y +'%,'+ z +')', function () {
});
});
*/
function getStyleSheet (title) {
var styleSheets = document.styleSheets;
for (var i = 0, l = styleSheets.length; i < l; i++) {
if (styleSheets[i].title === title) return styleSheets[i];
}
return null;
}
function getRulesList(title) {
var styleSheet = getStyleSheet(title);
return styleSheet.cssRules;
}
function setRule(title, selector, key, value, callback) {
var rules = getRulesList(title);
each(rules, function (rule) {
if (rule.constructor === CSSMediaRule) {
each(rule.cssRules, function (mRule) {
if (mRule.selectorText === selector) {
mRule.style[key] = value;
return callback();
}
});
} else if (rule.constructor === CSSStyleRule) {
if (rule.selectorText === selector) {
rule.style[key] = value;
return callback();
}
}
});
}
function getRule(title, selector, key, callback) {
var rules = getRulesList(title);
each(rules, function (rule) {
if (rule.constructor === CSSMediaRule) {
each(rule.cssRules, function (mRule) {
if (mRule.selectorText === selector && mRule.style[key]) return callback(mRule.style[key]);
});
} else if (rule.constructor === CSSStyleRule) {
if (rule.selectorText === selector && rule.style[key]) return callback(rule.style[key]);
}
});
}
function each (array, callback, scope) {
for (var i = 0, l = array.length; i < l; i++) {
var statment = callback.call(scope, array[i], i);
if (statment === 'break') break;
else if (statment === 'continue') continue;
}
}
// internal
function getTranslateValues (string) {
string = string.replace(/(-o-)|(-ms-)|(-moz-)|(-webkit-)|(translate)|(X)|(Y)|(3d)|(%)|(px)|(\()|(\))|(;)/g, '');
var values = string.split(',');
each(values, function (value, index) {
values[index] = value.trim();
values[index] = parseInt(values[index]);
});
return values;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.