Created
August 1, 2014 03:36
-
-
Save nealnote/b22db26217b4531fa4f2 to your computer and use it in GitHub Desktop.
requestAnimationFrame.html
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
<!DOCTYPE HTML> | |
<html> | |
<head> | |
<meta charset='utf-8'> | |
<title> requestAnimationFrame </title> | |
<style type="text/css"> | |
.mover { | |
position: absolute; | |
height: 100px; | |
width: 100px; | |
background-color: #27AE60; | |
} | |
</style> | |
</head> | |
<body> | |
<script type="text/javascript"> | |
var rAF = (function(){ | |
return window.requestAnimationFrame || function( callback ){ | |
window.setTimeout(callback, 1000/60) | |
} | |
})(); | |
for (var i = 0; i<50 ; i++ ){ | |
document.write("<div class='mover'></div>"); | |
} | |
var movers = document.querySelectorAll('.mover'); | |
(function(){ | |
for (var m = 0, l = movers.length; m < l ; m ++){ | |
movers[m].style.top = ( m * 20) + 'px'; | |
} | |
})(); | |
function update(timestamp){ | |
if ( timestamp < 5000 ) { | |
console.log( timestamp ) | |
} | |
for (var m = 0, l = movers.length; m < l ; m ++){ | |
movers[m].style.left = (Math.sin(m + timestamp/1000) + 1) * 500 + 'px'; | |
} | |
rAF(update); | |
} | |
rAF(update); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment