Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are my jQuery animations choppy in webkit-based browsers?

I'm working with a very simple animation: sliding a line of images to the left:

$('#button').click(
    function() {
        $('div#gallery').animate({scrollLeft:'+=800px'}, 1000);
    }
);

(It's supposed to be an image gallery; I hide the overflow to show only one image at a time.)

Anyway, despite trying various easing parameters, even at slow speeds the animation was very choppy in Chrome and Safari, but always smooth in Internet Explorer and Firefox. (Most issues raised online are about IE or Firefox being choppy!)

I found the cause, for me. It's a very special case that probably won't apply to most, but maybe it'll help someone regardless. I'll post my answer below. (The site guidelines allow this, I think.)

like image 953
slackwing Avatar asked Dec 17 '11 03:12

slackwing


People also ask

What is the default easing used for jQuery animation?

An easing function specifies the speed at which the animation progresses at different points within the animation. The only easing implementations in the jQuery library are the default, called swing , and one that progresses at a constant pace, called linear .

Is jQuery used for animation?

With jQuery, you can create custom animations.

Which jQuery function is used to add animation to an element?

animate() function in jQuery. Introduction: The jQuery animate() function performs custom animations on a set of CSS properties.


3 Answers

For me, it didn't make a difference if it was 24-bit PNGs, 8-bit PNGs, GIFs, JPEGs, etc., if there was a large image on the screen there were issues with my animations. If you have elements z-index'ed above a large image, try adding this CSS to your element:

-webkit-transform: translateZ(0);

For me, it worked, but it left animation artifacts.

What finally solved it for me was to simply change this:

$('#myimage').animate({
    height: 0,
    top: '-=50'
}, 500, 'linear');

To this:

$('#myimage').animate({
    height: '-=1'
}, 1, 'linear').animate({
    height: 0,
    top: '-=50'
}, 500, 'linear');

I just added a small 1 millisecond animation onto the beginning. My thinking was it would possibly "prep" Chrome for the real animation coming up, and it worked. You may want to change it to 20 or 50 milliseconds to be safe.

like image 131
Gavin Avatar answered Oct 09 '22 07:10

Gavin


In my case, the issue wasn't with the code, but with the images--specifically, large images that have been (forcibly) scaled down via the css width property. Of course larger images might take more processing to animate, but IE and FF seems to handle them just fine scaled down (in my case, images 2000px wide were scaled to be 800px wide). Meanwhile, it appears that Chrome and Safari get bogged down animating such images. Once I batch-shrunk all my images in Photoshop to actually be 800px wide, the animations were smooth everywhere.

Other details. I'm using jQuery 1.7. I encountered the issue for animating the img element itself, as well as a div element with a background image.

like image 28
slackwing Avatar answered Oct 09 '22 09:10

slackwing


I realize this thread is old, but in the interest of efficient coding..., I ran into a similar issue with choppy animations using Chrome recently and in searching for a solution, came across this thread(but not FF or IE) traced the issue to one of my collapse icons using a png as opposed to a gif (as soon as I swapped the png for a gif) the webkit browser had no issues producing smooth animations (but as soon as I swapped the png back in, my animations were once again choppy...)

like image 1
JAMES Avatar answered Oct 09 '22 09:10

JAMES