Skip to content

Instantly share code, notes, and snippets.

@ianpgall
Last active December 20, 2015 13:39
Show Gist options
  • Save ianpgall/6140706 to your computer and use it in GitHub Desktop.
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
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