Last active
May 3, 2019 21:36
-
-
Save honai/cc32e73821fd7434a6909bec25386158 to your computer and use it in GitHub Desktop.
JSのRequestAnimationFrameのサンプル
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
| <!DOCTYPE html> | |
| <html lang="ja"> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
| <title>RequestAnimationFrame</title> | |
| </head> | |
| <body> | |
| <div id="animate-div">アニメーション</div> | |
| </body> | |
| <script> | |
| var start = null; | |
| var element = document.getElementById('animate-div'); | |
| element.style.position = 'absolute'; | |
| function step(timestamp) { | |
| if (!start) start = timestamp; | |
| var progress = timestamp - start; | |
| element.style.left = Math.min(progress / 10, 200) + 'px'; | |
| if (progress < 2000) { | |
| window.requestAnimationFrame(step); | |
| } | |
| } | |
| window.requestAnimationFrame(step); | |
| </script> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment