Skip to content

Instantly share code, notes, and snippets.

@matthewbuchanan
Created April 15, 2014 05:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matthewbuchanan/10704875 to your computer and use it in GitHub Desktop.
Save matthewbuchanan/10704875 to your computer and use it in GitHub Desktop.
// Timelapse CSS functions by Karl von Randow, May 2008
var timelapseCss = function() {
var interval = 100;
function removeARule(attributes, state) {
if (state.k < attributes.length) {
var attribute = attributes[attributes.length - 1 - state.k];
var r = attributes.removeProperty(attribute);
if (!r) {
state.k++;
}
return true;
} else {
return false;
}
}
function disableARule(attributes, state) {
if (attributes == null) return false;
//state.i+=10;
if (state.k < attributes.length) {
var attribute = attributes[attributes.length - 1 - state.k];
var value = attributes.getPropertyValue(attribute);
var key = "i" + state.ipos + "j" + state.jpos;
if (state.disabled[key] == null) {
state.disabled[key] = [];
}
state.disabled[key].push({attribute:attribute, value:value});
return removeARule(attributes, state);
} else {
return false;
}
}
function enableARule(attributes, state) {
var key = "i" + state.ipos + "j" + state.jpos;
var d = state.disabled[key];
if (d != null && state.k < d.length) {
attributes.setProperty(d[d.length - 1 - state.k].attribute, d[d.length - 1 - state.k].value);
state.k++;
return true;
} else {
return false;
}
}
function iterateRules(action, state, async) {
while (true) {
if (state.i < document.styleSheets.length) {
state.ipos = state.forwards ? state.i : document.styleSheets.length - 1 - state.i;
var rules = document.styleSheets[state.ipos].cssRules;
if (state.j < rules.length) {
state.jpos = state.forwards ? state.j : rules.length - 1 - state.j;
var attributes = rules[state.jpos].style;
var cont = action(attributes, state);
if (cont) {
if (async)
setTimeout(function() {
iterateRules(action, state, async);
}, interval);
return true;
} else {
state.k = 0;
state.j++;
}
} else {
state.j = 0;
state.i++;
}
} else {
return false;
}
}
}
return {
timelapseRemoveCss: function() {
var state = {i:0, j:0, k:0, forwards:false};
iterateRules(removeARule, state, true);
},
removeCss: function() {
var state = {i:0, j:0, k:0, forwards:false};
while (iterateRules(removeARule, state, false));
},
timelapseApplyCss: function() {
var state = {i:0, j:0, k:0, forwards:false, disabled:[]};
while (iterateRules(disableARule, state, false));
state = {i:0, j:0, k:0, forwards:true, disabled:state.disabled};
iterateRules(enableARule, state, true);
}
};
}();
timelapseCss.timelapseApplyCss();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment