Skip to content

Commit

Permalink
requestAnimationFrame example
Browse files Browse the repository at this point in the history
  • Loading branch information
LeeMoonki committed Dec 28, 2020
1 parent d73cde2 commit ca80998
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions note/requestAnimationFrame.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>

<style>
#SomeElementYouWantToAnimate {
width: 50px;
height: 50px;
background-color: #ff0;
}
</style>
</head>
<body>
<div id="SomeElementYouWantToAnimate"></div>
<script type="text/javascript">
var start = null;
var element = document.getElementById('SomeElementYouWantToAnimate');
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>
</body>
</html>

0 comments on commit ca80998

Please sign in to comment.