Last active
December 20, 2015 13:39
-
-
Save ianpgall/6140706 to your computer and use it in GitHub Desktop.
JavaScript function that animates an element's numeric style in a certain amount of time
This file contains hidden or 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
var animate = (function () { | |
"use strict"; | |
var ret; | |
ret = function (elem, style, unit, from, to, time) { | |
var start, timer; | |
if (!elem) { | |
return; | |
} | |
if (time == null) { | |
time = 400; | |
} | |
start = new Date().getTime(); | |
timer = setInterval(function () { | |
var step = Math.min(1, (new Date().getTime() - start) / time); | |
elem.style[style] = (from + step * (to - from)) + unit; | |
if (step === 1) { | |
clearInterval(timer); | |
} | |
}, 25); | |
elem.style[style] = from + unit; | |
}; | |
return ret; | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment