//JSRUN引擎2.0,支持多达30种语言在线运行,全仿真在线交互输入输出。
const element = document.getElementById('animate');
let start;
function step(timestamp) {
if (start === undefined)
start = timestamp;
const elapsed = timestamp - start;
console.log(elapsed)
//这里使用`Math.min()`确保元素刚好停在200px的位置。
element.style.transform = 'translateX(' + Math.min(0.1 * elapsed, 200) + 'px)';
if (elapsed < 2000) { // 在两秒后停止动画
window.requestAnimationFrame(step);
}
}
window.requestAnimationFrame(step);
<div id="animate">
</div>
div{
width:40px;
height: 40px;
background-color: red;
}