views:

44

answers:

1

Hi

I need to optimize an image gallery slider since a lot of browsers have a hard time dealing with the animations.

Please consider the following example:

var $div1 = $('#div1'),
$div2 = $('#div2'),
$div3 = $('#div3'),
left = 0;

function animate() {
  left -= 10;
  $div1.css({
    left: left + 'px',
    width: 1000 - left + 'px'
  });
  $div2.css( 'left', left - 10 + 'px' );
  $div3.css( 'left', left - 40 + 'px' );
}

setInterval( animate, 20 );

This, of course, puts an immense pressure on a lot of browsers and it requires three repaints every 20 ms!

Is there any way to clone the three div's, work on them off-line and them replace ALL of them at once and thereby reduce the amount of repaints to one?

If you have other suggestions, please feel free to share them.

Thanks!

A: 

There are a few ways you can make this process more resource-efficient.

Idea 1: 20ms works out to 50 FPS. Your average Hollywood film runs at 24-30 FPS (I forget the exact frame rate). Try an interval of 33 - 40ms.

Idea 2: Use absolute positioning where possible to minimize the amount of reflowing required for the rest of the document.

Idea 3: Consolidate all of your animations into a single timer. I imagine most good frameworks will take care of this for you.

Ultimately, either you or your framework will be iteratively setting the CSS properties for each selector, so I think you will realize your gains by looking elsewhere.

C-Mo
Why the down vote? He asked for other ideas.
C-Mo
Thanks. Idea1: I could increase the speed, but it's still choppy in most browsers. Idea2: I forgot to say the divs are all absolutely positioned. Idea3: Single timer? I'm not sure I follow. Can you elaborate?
Morten
Here's the output from MozAfterPaint: http://pastebin.com/K3N0MZCW when doing the slide.
Morten
Sorry if I wasn't clear - I think I misunderstood part of your question. On idea 1, I actually meant to slow down the repaints from 50 times per second to 25-30. But your MozAfterPaint output indicates that you're still able to run faster than that.
C-Mo
Here's a screenshot of SpeedTracer. After the DOM click (where the animation starts) you can see 100 % repaints taking more than 20 ms. I guess that's the problem. That the repaints take longer than the interval itself. http://img412.imageshack.us/img412/7067/screenshot20100916at085.png
Morten
Maybe this can explain a potential bottle neck? http://img20.imageshack.us/img20/4701/screenshot20100916at090.png
Morten
I guess it's hard to say much more at this point without seeing an example of the page. Perhaps this SO thread can be of some help, as he was apparently trying to achieve something similar: http://stackoverflow.com/questions/2761379/jquery-animations-are-choppy-and-stutter-in-firefox
C-Mo