A Pen by Andrew Maxwell on CodePen.
Created
May 21, 2016 18:37
-
-
Save amaxwell01/cdbb4963159345df9237ff6eec372d8f to your computer and use it in GitHub Desktop.
requestAnimationFrame
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
<div id="SomeElementYouWantToAnimate"></div> |
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.getElementById("SomeElementYouWantToAnimate"); | |
var start = null; | |
function step(timestamp) { | |
var progress = null; | |
if (!start) { | |
start = timestamp; | |
console.log('Start: ', start); | |
} | |
// Show the current timestamp | |
console.log('Timestamp: ', timestamp); | |
// Update the progress to show the delta between the timestam and start time | |
progress = (timestamp - start); | |
console.log('Progress: ', progress); | |
// Update the left position | |
element.style.left = Math.min(progress / 10, 200) + "px"; | |
// Keep running the requestAnimationFrame until it reaches 2 seconds | |
if (progress < 2000) { | |
window.requestAnimationFrame(step); | |
} | |
} | |
/* | |
* requestAnimationFrame only accepts a single callback with a single return | |
* value. | |
* A parameter specifying a function to call when it's time to update your | |
* animation for the next repaint. The callback has one single argument, | |
* a DOMHighResTimeStamp, which indicates the current time | |
* (the time returned from Performance.now() ) for when requestAnimationFrame | |
* starts to fire callbacks. | |
*/ | |
window.requestAnimationFrame(step); |
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
#SomeElementYouWantToAnimate { | |
background-color: #bada55; | |
height: 40px; | |
position: absolute; | |
width: 40px; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment