Skip to content

Instantly share code, notes, and snippets.

@kenan-recebli
Created April 15, 2017 11:42
Show Gist options
  • Save kenan-recebli/627eaf2a62c4e09d383696f46eff7960 to your computer and use it in GitHub Desktop.
Save kenan-recebli/627eaf2a62c4e09d383696f46eff7960 to your computer and use it in GitHub Desktop.
Simple javascript animate function for old browsers
function animate(elem, options, duration, callback, callbackTimeout) {
var start = +new Date,
prop;
for (prop in options) {
var style = getComputedStyle(elem, ''),
value = options[prop],
px = style[prop].slice(-2),
currentValue = px == 'px' ? parseInt(style[prop]) : +style[prop],
unit = px == 'px' ? 'px' : '';
(function (a, b, c) {
return function _animate() {
var now = +new Date - start,
progress = now / duration,
result = (a - b) * progress + b;
elem.style[c] = progress < 1 ? result + unit : a + unit;
if (progress < 1) {
setTimeout(_animate, 10);
}
};
})(value, currentValue, prop)();
}
if (callback) {
setTimeout(callback, callbackTimeout || duration);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment