Considerations for CSS3 Transition Performance

守給你的承諾、 提交于 2019-12-12 07:28:55

问题


As part of a project that needs to support mobile devices, I have been working on mimicking the iPhone toggle control using CSS3. I have the look and feel of the element pretty much there, and am using CSS3 transitions to animate its state change.

When I have the element itself on a page with nothing else, the transition is relatively smooth on iOS. However, when I combine it with other CSS elements on a page, the result in iOS is laggy as anything. It's slightly better than a raw jQuery animation, but not much.

I've set up two test pages to demonstrate what I mean (the difference is hardly noticeable in a regular browser):

Toggle Control on its own > http://ben-major.co.uk/labs/iPhone%20UI/ios_toggle.html

Combined with other elements > http://ben-major.co.uk/labs/iPhone%20UI/

I am looking for any advice on speeding up the transition in mobile devices. What could be the factors that are slowing down its performance on the full page test?

Any advice and comments welcome.


回答1:


You have to be careful with this, as it can alter the z-index of the element it's applied to, but adding:

-webkit-transform-style: preserve-3d;

To the element you're applying the transition to, can speed animation up considerably, as it forces the hardware to use hardware acceleration for the animation.

If you do encounter layout bugs, you can just switch your 2d transitions to 3d values, so:

-webkit-transform: translate(100px, 100px)

becomes:

-webkit-transform: translate3d(100px, 100px, 0px)

You can see a demo of how this helps speed things up, at http://stickmanventures.com/labs/demo/spinning-gears-Chrome-preserve-3d/#

If after applying this to the element, you see it or elements around it blink upon use, then use:

-webkit-backface-visibility: hidden;

To the element, and that should correct the problem.

These tips have helped me to produce fast, efficient CSS transitions, hope they help. :)




回答2:


Chrome has recently improved the 2D transition performance, and now this trick is no longer needed. The best thing is that if removed the translate3d you'll no longer have those z-index problems! Use the test to prove. http://stickmanventures.com/labs/demo/spinning-gears-Chrome-preserve-3d/




回答3:


also you can try will-change: transform; , read more about it here: https://developer.mozilla.org/en-US/docs/Web/CSS/will-change#Browser_compatibility




回答4:


I think it quite old already but for anyone who still needs tricks to improve the transition performance on mobile device, you can apply : -webkit-transform: translateZ(0); to the element you are animating. This trick is according to this blog : http://chrissilich.com/blog/fix-css-animation-slow-or-choppy-in-mobile-browsers/. I have tried and it works quite well.



来源:https://stackoverflow.com/questions/7908493/considerations-for-css3-transition-performance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!