Created
October 3, 2013 16:30
-
-
Save tcard/6812768 to your computer and use it in GitHub Desktop.
Custom fadeTo.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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