Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Considerations for CSS3 Transition Performance

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.

like image 712
BenM Avatar asked Oct 26 '11 20:10

BenM


People also ask

How do I make transitions smoother in CSS?

With CSS3 we can specify how an element changes by just describing its current and target states. CSS3 will create a smooth transition between these states by applying a cubic Bézier curve and gradually change the element appearance.

Do CSS animations affect performance?

Compared with animating elements using JavaScript, CSS animations can be easier to create. They can also give better performance, as they give the browser more control over when to render frames, and to drop frames if necessary.

Why do you use CSS3 transitions?

CSS transitions provide a way to control animation speed when changing CSS properties. Instead of having property changes take effect immediately, you can cause the changes in a property to take place over a period of time.

What do think is the importance of using CSS transition and CSS animation?

Animations can build as many intermediate keyframes as necessary or desired. This gives you more control over your animation and allows you to create more complex and sophisticated animations. Transitions are for simple animations. Transitions don't change properties.


1 Answers

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. :)

like image 186
Dave Avatar answered Sep 30 '22 09:09

Dave