Well, you can still use .css() :
var anObj = $('.animatedObject'); anObj.css("width", anObj.css("width") - (20 / anObj.css("width"))).animate({ width: '+=20%' }, 1000);
I believe this will work faster.
Edit:
I made a small guide for you using jsperf.com . Here are my results (using Google Chrome ):
.animate({}, 0)
The code:
$('.animatedObject') .animate({width: '-=20%'}, 0) .animate({width: '+=20%'}, 1000);
The final results:
10,013 operations per second
±7.48%
fastest
.css();
The code:
var anObj = $('.animatedObject'); anObj.css("width", anObj.css("width") - (20 / anObj.css("width"))).animate({ width: '+=20%' }, 1000);
The final results:
2,477 operations per second
±6.39%
75% slower
Conclusion
Save the animation function. Turns out using .css() is actually slower. I assume you have 2 extra features. It is not worth it. This was a surprise to me, as I thought .css() would function faster than it was. Check it out yourself in your browser!
Shawn31313
source share