题目描述
requestAnimationFrame
回调函数中的参数是什么意思?还有就是官网这一段代码什么意思?
相关代码
const element = document.getElementById('some-element-you-want-
to-animate');
let start;
function step(timestamp) {
if (start === undefined)
start = timestamp;
const elapsed = timestamp - start;
//这里使用`Math.min()`确保元素刚好停在200px的位置。
element.style.transform = 'translateX(' + Math.min(0.1 * elapsed, 200) + 'px)';
if (elapsed < 2000) { // 在两秒后停止动画
window.requestAnimationFrame(step);
}
}
window.requestAnimationFrame(step);
具体参考MDN文档
callback
callback的参数是一个高精度时间戳(毫秒级),就是回调执行的时间戳。