Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Why is my CSS3/jQuery animation's performance so slow?

Why is my CSS3/jQuery animation's performance so slow?


Why oh why is this so slow? I have made an isometric grid of cubes with css3 that bobs up and down on mouseover. It works great on Firefox with a grid size less than about 12 and with Chrome works pretty good under about 18. I have a decent video card and CPU and the thing that is bugging me is why it is so slow to animate just one cube's animations if I make sure I only mouseover one cube. Does my JavaScript need optimisation or is this just to be expected from the current implementation of browser CSS3 and JavaScript engines? Full test case below includes slider to change grid size on the fly, can either download for yourself or visit this jsfiddle version graciously provided by Doug.

'); cube.css( { left : initialLeft + (20 * i) + (-19 * j) + 'px', top : initialTop + (11 * i) + (11 * j) + 'px' }); cube.find('.topFace div').css('background', 'rgb(100,' + Math.ceil(255 - 16 * i) + ',' + Math.ceil(255 - 16 * j) + ')'); cube.find('.rightFace').css('background', 'rgb(35,' + Math.ceil(190 - 16 * i) + ',' + Math.ceil(190 - 16 * j) + ')'); cube.find('.leftFace').css('background', 'rgb(35,' + Math.ceil(190 - 16 * i) + ',' + Math.ceil(190 - 16 * j) + ')'); cube.children('div').css('opacity', '.9'); cube.hover(function() { $(this).animate({top: '-=25px'}, 400, 'easeInCubic'); }, function() { $(this).animate({top: '+=25px'}, 400, 'easeOutBounce'); }); $('.box').append(cube); } } } $(document).ready(function() { $('#slider').slider( { value: 9, max: 30, min: 2, slide: refreshCubes, change: refreshCubes }); refreshCubes(); });
Problem courtesy of: SimonDever


This one is faster:

I optimized the jQuery code itself. Also, its a bad idea to animate a "+=25px" style CSS value because each time you do that you are forcing the browser to make extra CSS calculations on top of the animation calculations. Instead of doing that, you might as well just use plain CSS3 animations. Check this out:

Its better for animate() to have a static value (in this case, the original and raised positions which i've stored for each cube in the data- attributes) so the only calculations being done are the ones by the javascript interpreter itself.

The live() method automatically adds the events to the cubes whenever they are recreated, so there's no need to set the events inside the refreshcubes function. Also, James Montagne pointed out that using the live() method makes the whole thing behave like Daniel described in his answer (which is faster). EDIT: I've made it even faster by using the on() method instead of live(). The event context for hovering is now on each cube instead of on the whole document. See this question for details: How does jQuery's new on() method compare to the live() method in performance?.

Wherever possible, make one single jquery object and reference to it with a variable, that way you don't recreate a new object every time.

Overall, I noticed quite an increase in performance, but its still slower than I'd imagine capable. Maybe the CSS transforms are re-calculated every time the cubes are moved (even by a pixel).

EDIT: As addedlovely suggested, modifying the jQuery.fx.interval helped make it a little faster.

I think you'll have more luck drawing the cubes in 2D canvas or with 3D webGL canvas.

Try using three.js or processing.js to draw the cubes and animate them.

EDIT: I've made it even faster using hardware-accelerated CSS3 animations in Google Chrome (other browsers are still developing these CSS3 features so it only works in CHrome for now):

Solution courtesy of: trusktr


View additional discussion.

This post first appeared on CSS3 Recipes - The Solution To All Your Style Problems, please read the originial post: here

Share the post

Why is my CSS3/jQuery animation's performance so slow?


Subscribe to Css3 Recipes - The Solution To All Your Style Problems

Get updates delivered right to your inbox!

Thank you for your subscription