0

Suppose I have 10 elements on a page.

At the current moment I'm iterating them in a loop changing width and position:

el[i].style.width = ....
el[i].style.left = ....

I'm wondering if the animation could run faster if I tell the browser to re-render all elements only once, after the loop has finished

6
  • 1
    Are CSS3 or jQuery options? Commented Feb 4, 2013 at 18:47
  • Predefined CSS3 styles is not an option, since I'm writing a game with moving elements Commented Feb 5, 2013 at 7:45
  • 1
    You could use transitions... transition:all 1s linear; So when you change any property on the element, it will be transitioned to its new value. If need be, you could specify separate transitions for each property that you care about: transition:width 1s linear, left .5s ease-in-out; etc Commented Feb 5, 2013 at 16:48
  • jQuery is not an option. jQuery is 'code fast, play slow' Commented Apr 5, 2013 at 8:39
  • I want the page to be re-rendered after I have applied 20 CSS rules. By now page is re-rendered 20 times, sadly Commented Apr 5, 2013 at 8:41

3 Answers 3

1

Using CSS3 is faster and you will have to code a lot less, have a look at this Jsbin, I just did two examples, one with transition (.anima2) and the other with animation (.anima). With JS you just have to apply and remove the classes.

Sign up to request clarification or add additional context in comments.

Comments

1

If you apply the change to a shared class or load a style sheet with all changes in it this will make all changes apply at the same time, not sure about performance impact though...

1 Comment

I am calculating CSS rules using some formula, no chances to predict the numbers )
0

To be investigated by me (not fully cross browser, behaviour differs and changes, this needs heavy work of testing abd benchmarking):

a) requestAnimationFrame

b) CSS translate3d and other new CSS3 instead of left for GPU optimization

Come back later and I'll tell the results

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.