通过在vue3中用jsx的语法对比requestAnimationFrame和setTimeout
requestAnimationFrame浏览器自动控制运动的频率
setTimeout根据实际情况,一般人肉眼能识别的频率是60桢每秒,根据实际情况计算
setTimeout动画
setup() {
const boxRef = ref()
nextTick(() => {
let currWidth = 100
const maxWidth = 600
const animate = () => {
currWidth += 3
boxRef.value.style.width = currWidth + 'px'
if(currWidth < maxWidth) {
setTimeout(animate, 16.7)
}
}
animate()
})
return () => {
return (
<div>
<div ref={boxRef} className='box'></div>
</div>
)
}
}
requestAnimationFrame动画
setup() {
const boxRef = ref()
const maxWidth = 600
const animate = () => {
currWidth += 3
boxRef.value.style.width = currWidth + 'px'
if(currWidth < maxWidth) {
requestAnimationFrame(animate)
}
}
animate()
})
return () => {
return (
<div>
<div ref={boxRef} className='box'></div>
</div>
)
}
}