Skip to content

Instantly share code, notes, and snippets.

@TheSavior
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save TheSavior/25c93303962aeda23c16 to your computer and use it in GitHub Desktop.
Save TheSavior/25c93303962aeda23c16 to your computer and use it in GitHub Desktop.
Wealthfront Blog Embed RAF move
var element = document.getElementsByClassName('selector')[0];
var current = element.style.top;
var start = 0;
var goal = 100;
var duration = 2000;
var prevTime;
var tick = function(timestamp) {
if (!prevTime) {
prevTime = timestamp - 16.67; // set prevTime to a reasonable timestamp for the previous frame
}
current += (goal - start) * (timestamp - prevTime) / duration;
element.style.transform = 'translateY(' + current + 'px' + ')';
prevTime = timestamp;
if (current < goal) {
requestAnimationFrame(tick);
}
};
requestAnimationFrame(tick);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment