Make jQuery.animate run smoother with almost no code changes

Posted . Visible to the public.

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 });
}

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 .

Profile picture of Henning Koch
Henning Koch
Last edit
Henning Koch
License
Source code in this card is licensed under the MIT License.
Posted by Henning Koch to makandra dev (2015-01-09 10:19)