[英]Browser scrollbar animation performance
Why is this: 为什么是这样:
$('body').animate({scrollLeft: 1000});
much much faster than this: 比这快得多:
$('body').animate({"margin-left": 1000});
? ?
I am working on a website that has a animated scrolling. 我正在一个具有动画滚动的网站上工作。 eg: When the user clicks a link I fire a javascript that animates the scrollbar. 例如:当用户单击链接时,我会激活一个使滚动条带有动画效果的JavaScript。 Something similar to this website: 与此网站相似:
http://www.fashionphotographer.it/ http://www.fashionphotographer.it/
My first take on this problem was to animate the margin-left
using jQuery.animate but this proved to be very slow (my site is very content heavy). 我对这个问题的第一个想法是使用jQuery.animate margin-left
设置动画,但这被证明非常慢(我的网站内容非常繁琐)。 After that I tried animating the left
of a absolute element, using CSS3 and even the -webkit-transform. 之后,我尝试使用CSS3甚至-webkit-transform对绝对元素的left
进行动画处理。 All solutions where slow. 所有解决方案都很慢。
My last try was to use jQuery to animate the scrollbar, and this proved to be best solution so far. 我的最后尝试是使用jQuery为滚动条设置动画,到目前为止,这被证明是最好的解决方案。
My question is: What kind of optimization is the browser (I am using Chrome) doing under the hood that makes animating the scrollbar the best solution? 我的问题是:浏览器(我使用的是Chrome)在使滚动条动画化的最佳方案下做了什么样的优化?
Changing the scrollLeft property will not force a reflow of the DOM, as you are really just changing which portion of the content is visible at any one time. 更改scrollLeft属性不会强制DOM重排,因为您实际上只是在随时更改内容的哪一部分可见。 margin-left, left or other smiliar properties, on the other hand, may cause other elements to resize, which forces the browser to reflow the DOM. 另一方面,margin-left,left或其他辅助属性可能会导致其他元素调整大小,从而迫使浏览器重新排列DOM。
Edit: I believe scrollLeft will force a repaint , however this is much less intensive than a reflow. 编辑:我相信scrollLeft将强制重绘 ,但是这比重排要少得多。 See http://www.stubbornella.org/content/2009/03/27/reflows-repaints-css-performance-making-your-javascript-slow/ for a good explanation of the difference. 有关区别的详细说明,请参见http://www.stubbornella.org/content/2009/03/27/reflows-repaints-css-performance-making-your-javascript-slow/ 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.