-
-
Save TheSavior/25c93303962aeda23c16 to your computer and use it in GitHub Desktop.
Wealthfront Blog Embed RAF move
This file contains 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 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