Read more

Make jQuery.animate run smoother with almost no code changes

Henning Koch
January 09, 2015Software engineer at makandra GmbH

jQuery comes with .animate() Show archive.org snapshot that lets you transition some CSS selectors:

function floatIn($element) {
  $element.css({ 'opacity': 0, 'margin-top': 200px });
  $element.animate({ 'opacity': 1, 'margin-top': 0 }, { duration: 500 });
}
Illustration money motivation

Opscomplete powered by makandra brand

Save money by migrating from AWS to our fully managed hosting in Germany.

  • Trusted by over 100 customers
  • Ready to use with Ruby, Node.js, PHP
  • Proactive management by operations experts
Read more Show archive.org snapshot

The animation is implemented using setInterval and Javascript. This works great, but it's not as smooth as a CSS transition Show archive.org snapshot .

Fortunately the animate API can be mapped almost 1:1 to CSS transitions. There are libraries like Transit Show archive.org snapshot that act as a drop-in replacement for animate, but animate using CSS transitions instead of Javascript. This gives you a much higher framerate.

If Transit has too many bells and whistles for your taste, and you'd like to roll your own animate replacement, you might want to take a look at this Show archive.org snapshot .

Posted by Henning Koch to makandra dev (2015-01-09 11:19)