为什么 jQuery 不使用 requestAnimationFrame?
有些浏览器支持 requestAnimationFrame
,那么为什么不使用它呢?毕竟,自 Google Chrome 10 起就受到支持。尽管如此,jQuery 似乎并没有使用它。我找到了关于它的错误报告,但没有给出真正的解释?不过,我确信 jQuery 的人们有他们的理由。
他们为什么不使用这个很棒的 API?
Some browsers support requestAnimationFrame
, so why not use it? After all, it's been supported since Google Chrome 10. Despite that, jQuery does not seem to be using it. I've found a bug report about it, but no real explanation was given? I'm sure the jQuery people have their reasons, though.
Why wouldn't they use this awesome API?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 ticket #9381 中,您可以了解为什么他们在一段时间后停止使用
requestionAnimationFrame
。总而言之,问题在于,当窗口没有焦点时,动画不会运行(浏览器会尝试减少 CPU 负载),如果窗口是隐藏的,那么这是可以的,但如果窗口是可见的,只是在焦点之外,则不行。此外,动画队列堆积起来,在窗口重新获得焦点后,事情变得疯狂。这将需要对代码进行丑陋的更改和/或更改人们将内容添加到动画队列的方式。因此决定取消支持,直到有更好的方法来做到这一点。
In ticket #9381 you can read why they stopped using
requestionAnimationFrame
after some time.To summarize, problems were that animations didn't run (browsers try to reduce CPU load) when window didn't have focus, which is OK if the window is hidden, but not if it is visible, just out of the focus. Furthermore, animation queues piled up and after window regained focus, things went berserk. This would require ugly changes in the code and/or changes how people add things to the animation queue. So it was decided that support is removed until there is some better way to do this.
考虑到这里的其他答案和反对意见,我想在一个简单的幻灯片动画上测试一下:
http://brass9.com/nature< /a>
截至 2013 年,其他答案中的反对意见似乎不再重要。我添加了
https://github.com/gnarf /jquery-requestAnimationFrame/blob/master/src/jquery.requestAnimationFrame.js
添加到我现有的动画代码,并验证它是否打开并影响正在使用的淡入淡出动画。即使在后台窗口、Chrome 30、IE 11 和 FF 24 中,它也能可靠地工作。测试 Android 2.3,它似乎使用了 polyfill 并按预期工作。
jQuery 3
jQuery 3.0 集成了 requestAnimationFrame。基本上,jQuery 可以很好地处理它,但有些用户会调用
.setInterval(function() { tag.animate(
),搞砸了。因此,向主要版本发布的转折点。jQuery 3 不会,也将会从不支持 IE8 及更低版本,因此如果您有 IE8 用户,请坚持使用 jQuery 1.xCSS 转换
在我的测试中,
requestAnimationFrame
的 CPU/电池节省声明是虚假承诺。我看到它的 CPU 使用率很高,例如,长淡入淡出可以节省 CPU/电池的是 CSS。转换,可能是因为浏览器,尤其是移动浏览器,可以对您的意图以及对它们的其他要求做出更强有力的假设,并且本机代码仍然比 Javascript+DOM 更快,所以如果您真的想要的话 。保存CPU/电池、CSS 转换适合您,IE9 及以下版本无法处理它们,但仍有大量用户,因此请考虑 jquery.transit 及其回退到页面底部的
animate
。Given the other answers and objections here I wanted to test this out on a simple slideshow animation:
http://brass9.com/nature
As of 2013 the objections in other answers here no longer appear to be significant. I've added
https://github.com/gnarf/jquery-requestAnimationFrame/blob/master/src/jquery.requestAnimationFrame.js
to my existing animation code, and verified it's switching on and affecting the fade animations in use. It works reliably, even in background windows, in Chrome 30, IE 11, and FF 24. Testing Android 2.3, it appears to use the polyfill and work as expected.
jQuery 3
jQuery 3.0 integrates requestAnimationFrame. Basically, jQuery could handle it fine, but some users would call
.setInterval(function() { tag.animate(
, screwing it up. Thus the punt to major version release. jQuery 3 does not and will never support IE8 and below, so if you have IE8 users, stick with jQuery 1.x.CSS Transitions
In my testing, the CPU/battery-saving claims of
requestAnimationFrame
are false promises. I see high CPU usage with it on, for example, long-fades. What does save CPU/battery is CSS Transitions, probably because the browser, especially mobile browsers, get to make much stronger assumptions about what you intend and what else is being asked of them, and native code is still faster than Javascript+DOM.So if you really want to save CPU/battery, CSS Transitions are for you. IE9 and below can't handle them and there are still plenty of users out there, so consider jquery.transit and their Fallback to
animate
at the bottom of the page.在 v1.6.2 的 jQuery 源代码中,我看到使用了
requestAnimationFrame
(如果存在)。我还没有详细研究代码,没有看到它被用于它可以用于的所有用途,但它被用在代码的动画部分,而不是调用setInterval()
。这是 1.6.2 的代码:我还没有使用 1.6.4,所以我不知道该版本。如果它不在那个版本中,那么一定存在一些问题,因此它被删除了。
编辑:
如果您阅读这篇博文,听起来它已从 1.6.3 中撤出,也许会被放回到 1.7 中,它被撤回的主要原因是因为它破坏了人们“错误”使用动画队列的一些东西(尽管也许这就是一件事情的意见)。
In the jQuery source for v1.6.2, I see
requestAnimationFrame
being used if it's present. I haven't studied the code in great detail to see that it's being used for everything it could be used for, but it is being used in the animation section of the code instead of a call tosetInterval()
. Here's the code from 1.6.2:I am not yet using 1.6.4 so I don't know about that version. If it's not in that version, then there must have been some issues so it was removed.
EDIT:
If you read this blog post, it sounds like it was pulled out of 1.6.3 and perhaps will be put back in 1.7 and the main reason it was pulled is because it broke some things people were "incorrectly" using the animation queue for (though perhaps that is a matter of opinion).