Quinton Pike
Quinton Pike

Reputation: 3861

jQuery - Insert inline CSS or make a CSS Class and add

I have a question for the jQuery experts. I am trying to set a margin-left on a sliding div in a slider im making.

Is it faster for DOM Traversal and CSS rendering to do:

Insert inline css via .css('margin-left','-690px');

OR

Create a new css Class and insert it .addClass('marginLeftClass');

Does one have a potential advantage over another?

Upvotes: 1

Views: 3275

Answers (1)

Downpour046
Downpour046

Reputation: 1671

Adding the styles via .addClass is faster as the styles are already loaded.

By using .CSS() in jQuery you are injecting the values and making the element adapt to new CSS on the fly.

Its probably easier to think about it in larger terms, would you rather inject 10 seperate css properties on the fly via jQuery CSS properties, or just simply toggle a class? It's easier to add the class, and MUCH easier to manage.

Upvotes: 2

Related Questions