简体   繁体   English

使用jQuery fadeIn或CSS3动画更好吗?

[英]Is it better to use jQuery fadeIn or CSS3 animations?

I am creating a simple gallery using some PHP and JavaScript and am trying to do a fade transition between images. 我正在使用一些PHP和JavaScript创建一个简单的库,我正在尝试在图像之间进行淡入淡出过渡。 Then I wondered if there is a performance difference between using a CSS animation, eg: 然后我想知道使用CSS动画之间是否存在性能差异,例如:

@-webkit-keyframes fadeIn {
0%   { opacity: 0; }
100% { opacity: 1; }
}

and a jQuery fadeIn. 和一个jQuery淡入淡出。

I would like to use the callback from the fadeIn but I also can just use a timer with the CSS I guess. 我想使用fadeIn的回调,但我也可以使用带有CSS的计时器。

Are either of these likely to work better with large images? 对于大图像,这些中的任何一个都可能更好吗? I can't see a difference, but wondered if it might affect slower computers. 我看不出有什么不同,但想知道它是否会影响较慢的计算机。

How about one with a fallback to the other? 一个人倒退到另一个人怎么样? Use CSS3 transitions where possible, and use a feature detection library such as Modernizr to determine if the user's browser is capable of CSS3 transitions. 尽可能使用CSS3过渡,并使用诸如Modernizr之类的功能检测库来确定用户的浏览器是否能够进行CSS3过渡。

If AND ONLY IF the user's browser does not support native animations, only then should you use jQuery. 如果只有用户的浏览器不支持本机动画,那么只有你应该使用jQuery。

Native animations are GPU accelerated, resulting in less constraint on the CPU, and much smoother animations. 原生动画是GPU加速的,从而减少了对CPU的约束,并使动画更加流畅。 Also, IT DOESN'T REQUIRE JAVASCRIPT nor does it take extra requests to pull off. 此外,它不需要JAVASCRIPT,也不需要额外的请求。

Well, I think using CSS animation is a lot better when there is a supported browser and you should use jQuery only as a backup for unsupported browsers. 好吧,我认为当有支持的浏览器时,使用CSS动画要好得多,你应该只使用jQuery作为不支持的浏览器的备份。 As thoroughly explained on http://dev.opera.com/articles/view/css3-vs-jquery-animations , they conducted test of animating 300 divs at the same time with both CSS and jQuery, and noticed a huge difference between the performance statistics. 正如http://dev.opera.com/articles/view/css3-vs-jquery-animations中详细解释的那样,他们使用CSS和jQuery同时对300个div进行了动画测试,并注意到它们之间的巨大差异。绩效统计。

Statistics using CSS the animation were: 使用CSS动画的统计数据是:

    - Number of actions performed to finish the animation: 100 - 完成动画所执行的操作数:100
    - Time taken to finish executing the animation: 2.9 seconds - 完成动画执行所需的时间:2.9秒
    - Memory consumed at the end of the animation: 1.5 MB - 动画结束时消耗的内存:1.5 MB

and Statistics using jQuery were: 和使用jQuery的统计数据是:

    - Number of actions performed to finish the animation: 2119 - 完成动画所执行的操作数:2119
    - Time taken to finish executing the animation: 5 seconds - 完成动画执行所需的时间:5秒
    - Memory consumed at the end of the animation: 6 MB - 动画结束时消耗的内存:6 MB

So, as you can see, there is a great difference between the performance. 因此,正如您所看到的,性能之间存在很大差异。 This is because the browser's CSS processor is written in C++ and native code executes very fast whereas jQuery (JavaScript) is an interpreted language and the browser can't predict JavaScript ahead in time very well, in terms of what event will occur next, which restricts browsers to optimize it for performance. 这是因为浏览器的CSS处理器是用C ++编写的,本机代码执行速度非常快,而jQuery(JavaScript)是一种解释型语言,浏览器无法及时预测JavaScript,就接下来会发生什么事件而言,限制浏览器优化它以提高性能。 I hope that answers your question. 我希望能回答你的问题。

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

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