Improving UI Animation Workflow with Velocity.js

Avatar of Chris Coyier
Chris Coyier on (Updated on )

📣 Freelancers, Developers, and Part-Time Agency Owners: Kickstart Your Own Digital Agency with UACADEMY Launch by UGURUS 📣

The following is a guest post by Julian Shapiro. Julian recently released Velocity.js, a more performant jQuery replacement for .animate(). He recently wrote about how JavaScript animations can be so fast over on David Walsh’s blog, a topic we’ve covered here as well. In this article, Julian introduces Velocity.js itself.

Velocity.js is a jQuery plugin that re-implements jQuery’s $.animate() function to produce significantly higher performance (making Velocity also faster than CSS transitions in many cases) while including several new features to improve animation workflow.

In 7Kb (zipped), Velocity includes all of $.animate()‘s features while also packing in transform animation, looping, class animation, and scrolling. In short, Velocity is designed to be the best of jQuery, jQuery UI, and CSS transitions.

Velocity works everywhere — back to IE8 and Android 2.3. Further, since Velocity’s syntax is identical to $.animate()‘s, none of your page’s code needs to change.

The goal of Velocity is to be a leader in DOM animation performance and convenience. This article focuses on the latter. To learn more about the former, refer to Velocity’s performance comparisons over at VelocityJS.org. In particular, this article demonstrates how to use Velocity to improve your UI animation workflow. In a concise showdown, eight of Velocity’s features are compared against their jQuery counterparts.

If you feel that your current UI workflow is messy, poorly segregated, or too reliant upon jQuery’s broad array of style functions, then this walkthrough is for you.

Brief Overview

Before we dive into Velocity, let’s quickly cover the bases: It should be pointed out that both $.animate() and $.velocity() support a malleable options syntax. You can either pass in options as comma-separated values, or you can pass in a standalone options object.

Here’s an example of the comma-separated syntax, in which an integer is treated as the animation’s duration, a string is treated as the easing type, and a function is treated as a callback (which is triggered upon the animation’s completion):

$div.animate(
  { 
    opacity: 1 
  }, 
  1000, 
  "linear", 
  function() { 
    alert("Done animating."); 
  }
);

Next, here’s an example of the object syntax:

$div.animate(
  { 
    opacity: 1 
  }, 
  { 
    duration: 1000, 
    easing: "linear", 
    complete: function() { 
      alert("Done animating!") 
    }, 
    queue: "myQueue" 
  }
);

Beyond producing cleaner-looking code, using the options object provides access to additional animation parameters that are otherwise not able to be specified via the comma-separated syntax.

An example of one such option provided by jQuery is “queue”. Velocity also provides the queue option, and multiple animation calls chained onto a single element automatically queue onto one another.

Here, a div’s opacity will be animated to 1 for 1000ms then back down to 0 for the next 1000ms:

$div
  .animate({ opacity: 1 }, 1000)
  .animate({ opacity: 0 }, 1000);

Now, with the bases out of the way, let’s start comparing Velocity to jQuery.

Reversing

In place of a properties map, Velocity also accepts "reverse" as its first parameter. Reverse animates the target element toward its values prior to its previous Velocity call.

In jQuery, that would be like:

$div
  /* Fade an element in while sliding it into view. */
  .animate({ opacity: 1, top: "50%" })
  /* The values below are what we originally set the element to in our stylesheet. Animate back to them. */ 
  .animate({ opacity: 0, top: "-25%" });

In Velocity, it’s easier as it is not only less code, but you don’t have to repeat values from your stylesheet:

$div
  .velocity({ opacity: 1, top: "50%" })
  .velocity("reverse");

By default, Velocity’s reverse command uses the same options that were passed into the previous Velocity call. These options can be extended by passing new options into the “reverse” call. For example:

$div
  .velocity({ opacity: 1, top: "50%" }, 1000)
  /* Animate back to the prior visual state at half the duration of the previous animation. */
  .velocity("reverse", 500);

Scrolling

A popular UI technique is to scroll the browser so that it’s aligned with an element further down the page, then to animate that element with attention-grabbing flourishes. Pulling this off with jQuery involves messy, non-performant code:

In jQuery, animating the scrollTop property requires you to target both the html element and the body element in order for the animation to work in older versions of Internet Explorer.

$("html, body").animate(
  { 
    scrollTop: $div.offset().top 
  }, 
  1000, 
  function() {
    /* We use a callback to fade in the div once the browser has completed scrolling. */
    $div.animate({ opacity: 1 });
  }
);

In Velocity, you target the element you want to scroll to:

$div
  .velocity("scroll", 1000)
  .velocity({ opacity: 1 });

Just as with Velocity’s “reverse” command, “scroll” can be passed in as Velocity’s first parameter in place of a properties map. Also like the reverse command, the scroll command accepts animation options and can be chained onto other calls.

The scroll command’s behavior is straightforward: Scroll the browser to the top of the element targeted by the Velocity call.

Looping

Oftentimes, an element’s animation needs to be looped. Examples include shaking a dialog box to indicate invalid user input or bouncing a notification icon to grab the user’s attention.

In jQuery, looping an animation entails messing your animation logic by breaking part of it out into a for statement:

for (var i = 0; i < 5; i++) {
  $div
    /* Slide the element up by 100px. */
    .animate({ top: -100 })
    /* Then animate back to the original value. */
    .animate({ top: 0 });
}

In Velocity, simply set the loop option to an integer equal to the desired number of loop cycles. A single loop cycle consists of animating toward the values in the property map followed by reversing back to the original values.

$div.velocity(
  { top: -100 }, 
  { loop: 5 }
);

Fading Elements

You’ll often find yourself fading in an element whose display property was initially set to none, so that the element wasn’t immediately visible upon page load. Subsequently fading in these elements requires several lines of jQuery:

$div
  /* Use jQuery's $.show() function to make the element visible by switching its display property to "block"/"inline" as appropriate. */
  .show()
  /* Set the element's starting opacity to 0 so that it can be gradually faded in by the subsequent animation call. */
  .css("opacity", 0)
  /* Fade in and slide into view. */
  .animate({ 
    opacity: 1, 
    top: "50%" 
  });

In Velocity, you simply pass in display as an option. The display option accepts the same set of values that its CSS property counterpart does (e.g. “block”, “inline”, and “none”).

$div.velocity(
  { 
    opacity: 1, 
    top: "50%" 
  },
  { 
    display: "block" 
  }
);

When the display option is set to a value other than none, the element’s display property is set to the provided value at the start of the animation. Conversely, when display is passed a value of none, the display property is set upon the animation’s completion.

$div
  /* Fade out and slide out of view. */
  .animate({ opacity: 0, top: "-50%" })
  /* Then set the display property to "none" via a queued $.fadeOut() call. */
  .fadeOut(1);
$div.velocity(
  { 
    opacity: 0,
    top: "-50%" 
  }, 
  { 
    display: "none" 
  }
);

Further, if an element’s opacity is being animated to a non-zero value while its display option is being set to a value other than none, Velocity conveniently defaults opacity‘s start value to 0.

Delaying

Velocity accepts a delay option that replaces having to sprinkle $.delay() calls throughout your animation code:

$div
  .delay(1000)
  .animate({ 
    opacity: 1 
  });
$div.velocity(
  { 
    opacity: 1 
  }, 
  { 
    delay: 1000 
  }
);

Beyond consolidating animation logic into a single call, using Velocity’s built-in delay option allows Velocity to optimize chained animations by caching values between them.

Sequences

A feature unique to Velocity is sequences, which are macros for animations: An animation sequence is created once, then it can be subsequently triggered when desired across elements on all your pages.

The benefits of sequences include:

  • Naming your animations for better code organization.
  • Separating UI animation logic from UI interaction logic.
  • Packaging animations for sharing across your projects and with other developers.

Sequences are created by extending the $.Velocity.Sequences object. They are thereafter triggered by passing in the sequence’s name as Velocity’s first argument.

Below is a simple “hover” sequence:

$.Velocity.Sequences.hover = function (element, options) {
  var duration = options.duration || 750;

  $.Velocity.animate(element,
    { 
      translateY: "-=10px",
    }, { 
      /* Delay is relative to user-adjustable duration. */
      delay: duration * 0.033,
      duration: duration,
      loop: 3,
      easing: "easeInOutSine"
    });
};

Now, you can apply it to an element:

$div.velocity("hover");

In the above sequence, the element translates up then down by 10px three times (loop: 3) with a short intermittent delay.

To learn more about the nuances of sequences, read the documentation of it.

Velocity uses its own sequences feature to pre-package in fadeIn, fadeOut, slideUp, and slideDown functions (which are identical to their jQuery equivalents).

For example, slide a container div down into view:

$div.velocity("slideDown", function() {
  /* Then fade in its children over a duration of 1000ms. */
  $children.velocity("fadeIn", 1000);
});

Forced Hardware Acceleration

Forcing hardware acceleration (HA) on an element is an easy to way to dramatically increase animation performance on mobile devices. Enabling HA is traditionally achieved by setting an element’s transform property to translateZ(0).

$div
  .css("transform", "translateZ(0)")
  .animate({ opacity: 1 })
  .css("transform", "none");

In Velocity, HA is automatically applied on mobile devices (there’s no performance boost to be gained on the desktop). Velocity’s control over HA is highly optimized.

$div.velocity({ opacity: 1 });

Wrapping Up

The purpose of this walkthrough has been to demonstrate Velocity’s consolidation of animation logic within your code. In short, Velocity is an expressive and efficient tool for crafting UI animations.

While jQuery is tremendously powerful, it was never jQuery’s design goal to function as an optimized animation engine, and it suffers accordingly from less-than-ideal performance and workflows. At just 7Kb zipped, Velocity packs in enough features and speed gains for you to consider making it your animation engine of choice.

To explore the remainder of Velocity’s features, including color and transform animation, check out Velocity’s documentation at VelocityJS.org.

Before we conclude, here are some extreme examples of web animation that are made possible through the expressiveness and speed of Velocity: