【发布时间】:2022-01-12 06:54:42
【问题描述】:
我有以下功能,通过调整每个“滴答”的样式,将某些元素“向上”滚动到视野之外:
const incr = 1;
let moved = 0;
function changeHeight( children, duration, setTop) {
// duration = 1500
const height = children.clientHeight; // in this case, 166px
let moved = 0;
const slideUp = function (timestamp) {
// we're done if the amount moved is larger than height
if ( moved < height ) {
children.style.top = `${ setTop( moved, height ) }px`;
moved = moved + incr // move by some amount
requestAnimationFrame(slideUp)
} else {
// reset
moved = 0;
}
};
// start sliding
slideUp();
}
如果requestAnimationFrame 大约每 16 毫秒触发一次,我想使用duration 来指示动画将运行多长时间,所以公式似乎是height * 16.5 / duration
我对@987654325@ 感到困惑 - 为什么每次滴答的时间不是恒定的?我想使用由requestAnimationFrame 生成的timestamp,但前几个周期花费的时间远比平均约16.5
16.5 在不同的机器或屏幕上看起来会有所不同吗?
如何使高度变化完全按照指定的时间量进行?
【问题讨论】:
标签: javascript animation requestanimationframe