let boxs = document.querySelectorAll(".box"); let left = 0; let flag = 1; // setTimeout动画 setTimeout(functionanimation() { if (flag) { left += 1; } else { left -= 1; } if (left >= 200) { flag = 0; } elseif (left <= 0) { flag = 1; } for (let i = 0; i < boxs.length; i++) { let box = boxs[i] box.style.left = left + "px"; } setTimeout(animation, 16.7) }, 16.7)
</script> </body> </html>
我们使用Perforence来进行观测
我们可以看到,每一帧的时间都不相同,我的屏幕是60hz的,如果希望动画能尽可能流畅,那就需要每一帧要在1000 ms / 60 = 16.7ms之内渲染出来,但是setTimeout显然不能满足这个需求
let boxs = document.querySelectorAll(".box"); let left = 0; let flag = 1; // // requestAnimationFrame动画 requestAnimationFrame(functionanimation() { if (flag) { left += 1; } else { left -= 1; } if (left >= 200) { flag = 0; } elseif (left <= 0) { flag = 1; } for (let i = 0; i < boxs.length; i++) { let box = boxs[i] box.style.left = left + "px"; } requestAnimationFrame(animation); })
// 一些计算 for (let i = 0; i < 10; i++) { setTimeout(functioncalSum() { console.log(`计算执行${i}`); let sum = 0; for (let i = 0; i < 300000; i++) { sum += i; } }, 0) } let count = 10; requestAnimationFrame(functionanimation() { console.log("动画"); count-- && requestAnimationFrame(animation); })
<!DOCTYPE html> <htmllang="en"> <head> <metacharset="UTF-8"> <title>Title</title> <style> .box { width: 100px; height: 100px; background: yellow; border: 5px solid cornflowerblue; box-sizing: border-box; position: relative; /* 使用CSS动画 */ animation: boxFrames1 infinite 5s linear; } @keyframes boxFrames1 { 0%, 100% { position: relative; left: 0; } 50% { position: relative; left: 200px; } } </style> </head> <body> <divclass="box">123</div> <script> for (let i = 0; i < 1000; i++) { setTimeout(functioncalSum() { console.log(`计算执行${i}`); let sum = 0; for (let i = 0; i < 300000; i++) { sum += i; } }, 0) }
let boxs = document.querySelectorAll(".box"); let left = 0; let flag = 1; // 使用requestAnimationFrame做JS动画 requestAnimationFrame(functionanimation() { if (flag) { left += 1; } else { left -= 1; } if (left >= 200) { flag = 0; } elseif (left <= 0) { flag = 1; } for (let i = 0; i < boxs.length; i++) { let box = boxs[i] box.style.left = left + "px"; } requestAnimationFrame(animation); }) </script>
</style> </head> <body> <divclass="box">123</div> <divclass="box">123</div> <divclass="box">123</div> <divclass="box">123</div> <divclass="box">123</div> <divclass="box">123</div> <divclass="box">123</div> <divclass="box">123</div> <divclass="box">123</div> <divclass="box">123</div> <divclass="box">123</div> <divclass="box">123</div> <divclass="box">123</div> <divclass="box">123</div> <divclass="box">123</div> <divclass="box">123</div> <divclass="box">123</div> <divclass="box">123</div> <divclass="box">123</div> <divclass="box">123</div> <divclass="box">123</div> <divclass="box">123</div> <divclass="box">123</div> <divclass="box">123</div> <divclass="box">123</div> <divclass="box">123</div> <divclass="box">123</div> <divclass="box">123</div> <script> setInterval(functioncalSum() { let sum = 0; for (let i = 0; i < 500000; i++) { sum += i; } }, 10)
let boxs = document.querySelectorAll(".box"); let left = 0; let flag = 1; // requestAnimationFrame动画 requestAnimationFrame(functionanimation() { if (flag) { left += 1; } else { left -= 1; } if (left >= 200) { flag = 0; } elseif (left <= 0) { flag = 1; } for (let i = 0; i < boxs.length; i++) { let box = boxs[i] box.style.left = left + "px"; } requestAnimationFrame(animation); })