Skip to content

Instantly share code, notes, and snippets.

@honai
Last active May 3, 2019 21:36
Show Gist options
  • Select an option

  • Save honai/cc32e73821fd7434a6909bec25386158 to your computer and use it in GitHub Desktop.

Select an option

Save honai/cc32e73821fd7434a6909bec25386158 to your computer and use it in GitHub Desktop.
JSのRequestAnimationFrameのサンプル
<!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