1

I am using pure javascript (no jquery or any other framework) for animations. Which one's more optimized? Creating classes for transition like:

CSS class:

    .all-div-has-this-class { transition: all 1s; }
    .class1 { left: 0; }
    .class2 { left: 30px; }

Javscript:

    testdiv.className += " class1";
    testdiv.className += " class2";

or just this in javascript => Initialize the testdiv position in the css then just dotestdiv.style.left = "30px"; in the js code?

By the way, these are all in setTimeout functions to set the properties according to timing. Also, it has to be only javascript without any jquery or any other framework specifically for animations.

2 Answers 2

1

Dom manupulation is quite heavy as always, particular if you want to do a lot animation using JS, that's why we have CSS anamition which use browser buildin function to render anamation.

Js code given just assgin css class to dom, and the rules are caculate by browser based on the css classes

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

4 Comments

How does this answer the question?
@jfriend00, saw you yesterday on another question, bro! what the question ask is not clear, i am just trying get close
So, by just adding and removing classes of an element is more optimized than changing a particular style of an element? mmhmm.. I guess I have to refactor my codes then. I change the property of the element through dom directly and not through changing css classes...
@Don, yep , you got this right, please tick my answer, bro! lol
1

There won't be a general answer for all browsers and all hardware, but this answer did a great experiment changing text-align of 10000 elements using javascript to modify.style vs. using CSS.

Summarizing the significant milliseconds you can see CSS is nearly twice as fast:

script CSS activity
34 0 Javascript
104 107 Layout
16 16 Update Layer Tree
82 32 Recalculate Style

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.