Skip to content

Instantly share code, notes, and snippets.

@tcard
Created October 3, 2013 16:30
Show Gist options
  • Save tcard/6812768 to your computer and use it in GitHub Desktop.
Save tcard/6812768 to your computer and use it in GitHub Desktop.
Custom fadeTo.
// Like the fadeTo from jQuery, but doesn't wait until the previous
// animation is over.
var fadeTo = (function() {
var fadingStep = 13;
return function(jqSel, time, toOpacity) {
jqSel.each(function(i, elem) {
if (elem.fadingState != null) {
clearInterval(elem.fadingState.interval);
}
elem.fadingState = {elem: elem, toOpacity: toOpacity, time: time,
interval: setInterval(function() {
var st = elem.fadingState;
var el = $(elem);
var currOpacity = parseFloat(el.css('opacity'));
var rest = toOpacity - currOpacity;
newOp = currOpacity + (rest / (st.time / 13));
if (Math.abs(newOp-toOpacity) >= Math.abs(rest)) {
clearInterval(st.interval);
delete elem['fadingState'];
return;
}
el.css('opacity', newOp);
st.time -= fadingStep;
}, fadingStep),
}
});
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment