Created
April 15, 2017 11:42
-
-
Save kenan-recebli/627eaf2a62c4e09d383696f46eff7960 to your computer and use it in GitHub Desktop.
Simple javascript animate function for old browsers
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
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