JSFiddle - React, Tailwind, and code Playground
by knowonline
HTML
<p>Статья: <a href="https://know-online.com/post/javascript-requestanimationframe" target="_blank">JavaScript. Анимация (requestAnimationFrame)</a></p>
<div id="el" class="el"></div>
<!-- Пример сайта know-online.com -->
CSS
.el {
background: #000;
width: 100px;
height: 100px;
position: absolute;
}
body {
font-family: sans-serif;
}
JavaScript
var el = document.getElementById("el");
function moveElement() {
var curPos = parseInt(el.style.left) + 1 || 0;
el.style.left = curPos+"px";
if (curPos < 150) {
requestAnimationFrame(moveElement);
}
}
requestAnimationFrame(moveElement);