I watched a video describing how to use requestAnimationFrame for looping animation. I tried to reproduce it and I think that I am very close, but I miss the key point. When I click "field", onclick = "loopKeyFrame ()" is activated and calls runKeyFrame () only once?
My intention was to have the runKeyframe () loop endlessly with this code. I know that I can put "infinite" in css so that it runs forever, but that is not the point. im trying to use requestAnimationFrame as was used in the video.
My code in CodePen
<html>
<head>
<title>RequestAnimation Lesson</title>
<style>
body {
background-color: grey;
}
#box {
position:relative;
width:30px;
border:1px solid blue;
background-color:blue;
height:10px;
top:0px;
left:0px;
}
@keyframes myMove {
0%{left:0%;}
50%{left:95%;}
100%{left:0%;}
}
</style>
<script>
function runKeyFrame() {
document.getElementById("box").style.animation = "myMove 4s";
}
function loopKeyFrame() {
runKeyFrame();
requestAnimationFrame(loopKeyFrame);
}
</script>
</head>
<body>
<div id="box" onclick="loopKeyFrame()"></div>
</body>
</html>
source
share