简体   繁体   English

什么时候使用 requestAnimationFrame? 如果我们有 css 过渡,为什么我们需要 requestAnimationFrame ?

[英]When to use requestAnimationFrame? and why we need requestAnimationFrame if we have css transition?

I don't know When to use requestAnimationFrame.我不知道什么时候使用 requestAnimationFrame。 If requestAnimationFrame and css both can satisfy my needs, which one should I choose?如果 requestAnimationFrame 和 css 都可以满足我的需求,我应该选择哪一个?

can you show me some of your Applied Cases?你能告诉我一些你的应用案例吗?

thx very much~非常感谢~

I think the only reason to opt for requestAnimationFrame over CSS animations is when you want to do quick animations where you need to ensure that browsers would make it smooth.我认为选择requestAnimationFrame不是 CSS 动画的唯一原因是当您想要制作快速动画时,您需要确保浏览器使其流畅。 Animations like jump, stop, pause, return could benefit from using requestAnimationFrame .跳转、停止、暂停、返回等动画可以从使用requestAnimationFrame受益。

The thing is that requestAnimationFrame uses no CPU power, and only draws animation that would be visible to the user (it doesn't consume anything if you swich tabs, minimize, etc).问题是requestAnimationFrame使用 CPU 功率,并且只绘制用户可见的动画(如果您切换选项卡、最小化等,它不会消耗任何内容)。 Frames would be only drawn when the browser is ready and since there are no ready frames waiting to be drawn, there are no unnecessary frame draws.只有当浏览器准备好时才会绘制帧,并且由于没有等待绘制的就绪帧,因此没有不必要的帧绘制。 So, for smoothness guarantee, you can safely opt for requestAnimationFrame .因此,为了保证流畅性,您可以放心地选择requestAnimationFrame

If you plan something simple that doesn't fit the above explanation, then requestAnimationFrame is overkill and you'd better switch to CSS animations.如果你计划了一些不符合上述解释的简单计划,那么requestAnimationFrame就有点矫枉过正了,你最好切换到 CSS 动画。

Source consulted:参考资料来源:

https://dev.opera.com/articles/better-performance-with-requestanimationframe/ https://dev.opera.com/articles/better-performance-with-requestanimationframe/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM