{"id":168883,"date":"2014-04-30T08:54:26","date_gmt":"2014-04-30T15:54:26","guid":{"rendered":"http:\/\/css-tricks.com\/?p=168883"},"modified":"2014-05-18T01:55:42","modified_gmt":"2014-05-18T08:55:42","slug":"improving-ui-animation-workflow-velocity-js","status":"publish","type":"post","link":"https:\/\/css-tricks.com\/improving-ui-animation-workflow-velocity-js\/","title":{"rendered":"Improving UI Animation Workflow with Velocity.js"},"content":{"rendered":"
The following is a guest post by Julian Shapiro<\/a>. Julian recently released Velocity.js, a more performant jQuery replacement for <\/p>\n Velocity.js<\/a> is a jQuery plugin that re-implements jQuery’s In 7Kb (zipped), Velocity includes all of Velocity works everywhere \u2014 back to IE8 and Android 2.3. Further, since Velocity’s syntax is identical to 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<\/a>. 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.<\/p>\n If you feel that your current UI workflow is messy, poorly segregated, or too reliant upon jQuery\u2019s broad array of style functions, then this walkthrough is for you.<\/p>\n Before we dive into Velocity, let’s quickly cover the bases: It should be pointed out that both 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):<\/p>\n Next, here’s an example of the object syntax:<\/p>\n 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.<\/p>\n 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.<\/p>\n Here, a div’s opacity will be animated to 1 for 1000ms then back down to 0 for the next 1000ms:<\/p>\n Now, with the bases out of the way, let’s start comparing Velocity to jQuery.<\/p>\n In place of a properties map, Velocity also accepts In jQuery, that would be like:<\/p>\n In Velocity, it’s easier as it is not only less code, but you don’t have to repeat values from your stylesheet:<\/p>\n 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:<\/p>\n 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:<\/p>\n In jQuery, animating the In Velocity, you target the element you want to scroll to:<\/p>\n 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.<\/p>\n The scroll command’s behavior is straightforward: Scroll the browser to the top of the element targeted by the Velocity call.<\/p>\n 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.<\/p>\n In jQuery, looping an animation entails messing your animation logic by breaking part of it out into a for statement:<\/p>\n 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.<\/p>\n You’ll often find yourself fading in an element whose display property was initially set to 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”).<\/p>\n When the Further, if an element’s Velocity accepts a delay option that replaces having to sprinkle 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.<\/p>\n A feature unique to Velocity is sequences<\/em>, 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.<\/p>\n The benefits of sequences include:<\/p>\n 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.<\/p>\n Below is a simple “hover” sequence:<\/p>\n Now, you can apply it to an element:<\/p>\n In the above sequence, the element translates up then down by 10px three times (loop: 3) with a short intermittent delay.<\/p>\n To learn more about the nuances of sequences, read the documentation of it<\/a>.<\/p>\n Velocity uses its own sequences feature to pre-package in For example, slide a container div down into view:<\/p>\n 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 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. <\/p>\n 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.<\/p>\n 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.<\/p>\n To explore the remainder of Velocity’s features, including color and transform animation, check out Velocity’s documentation at VelocityJS.org.<\/p>\n Before we conclude, here are some extreme examples of web animation that are made possible through the expressiveness and speed of Velocity:<\/p>\n 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.<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"sig_custom_text":"","sig_image_type":"featured-image","sig_custom_image":0,"sig_is_disabled":false,"inline_featured_image":false,"c2c_always_allow_admin_comments":false,"footnotes":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":[]},"categories":[4],"tags":[],"jetpack_publicize_connections":[],"acf":[],"jetpack_featured_media_url":"","jetpack-related-posts":[{"id":177678,"url":"https:\/\/css-tricks.com\/clever-uses-step-easing\/","url_meta":{"origin":168883,"position":0},"title":"Clever Uses for Step Easing","date":"August 5, 2014","format":false,"excerpt":"The following is a guest post by Julian Shapiro. Julian has been working on Velocity.js recently, which he has written about here on CSS-Tricks before. Julian lives in a world of animation, so it's no surprise he's collected together some interesting examples of exotic animation techniques. Here he shares all\u2026","rel":"","context":"In "Article"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":193135,"url":"https:\/\/css-tricks.com\/weighing-svg-animation-techniques-benchmarks\/","url_meta":{"origin":168883,"position":1},"title":"Weighing SVG Animation Techniques (with Benchmarks)","date":"January 27, 2015","format":false,"excerpt":"The following is a guest post by Sarah Drasner (@sarah_edo). Sarah has been researching and giving talks about animation lately. I jumped at the chance to have her share some of that research here, this time focusing on SVG animation and the different tech choices you can make to do\u2026","rel":"","context":"In "Article"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":160266,"url":"https:\/\/css-tricks.com\/myth-busting-css-animations-vs-javascript\/","url_meta":{"origin":168883,"position":2},"title":"Myth Busting: CSS Animations vs. JavaScript","date":"January 13, 2014","format":false,"excerpt":"The following is a guest post by Jack Doyle, author of the GreenSock Animation Platform (GSAP). Jack does a lot of work with animations in the browser and has discovered that the generic opinion that \"CSS is faster\" just isn't true. It's more than that, as well. I'll let him\u2026","rel":"","context":"In "Article"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":312080,"url":"https:\/\/css-tricks.com\/an-overview-of-scroll-technologies\/","url_meta":{"origin":168883,"position":3},"title":"An Overview of Scroll Technologies","date":"June 22, 2020","format":false,"excerpt":"Scroll-related animations have been used on the web for years. In recent years, they\u2019ve started to become more common, perhaps in part due to devices being higher-performing and thus able to handle more animation.\u00a0 There are a number of scroll-related technologies out there, so this article\u2019s aim is to provide\u2026","rel":"","context":"In "Article"","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2019\/03\/scroll-position.png?fit=1200%2C600&ssl=1&resize=350%2C200","width":350,"height":200},"classes":[]},{"id":179204,"url":"https:\/\/css-tricks.com\/add-page-transitions-css-smoothstate-js\/","url_meta":{"origin":168883,"position":4},"title":"How To Add Page Transitions with CSS and smoothState.js","date":"August 15, 2014","format":false,"excerpt":"CSS animations (and a little JavaScript trickery) can let us add page transitions and move away from the hard-cut of page loads. My jQuery plugin smoothState.js helps polish those transitions and improve UI response times. Page transitions benefit the user experience Imagine, for a second, how disorienting it would be\u2026","rel":"","context":"In "Article"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":186041,"url":"https:\/\/css-tricks.com\/guide-svg-animations-smil\/","url_meta":{"origin":168883,"position":5},"title":"A Guide to SVG Animations (SMIL)","date":"October 13, 2014","format":false,"excerpt":"Update December 2015: At the time of this update, SMIL seems to be kinda dying. Sarah Drasner has a guide on how you can replace some of it's features. The following is a guest post by Sara Soueidan. Sara has a knack for digging deep into web features and explaining\u2026","rel":"","context":"In "Article"","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2022\/08\/path.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]}],"featured_media_src_url":null,"_links":{"self":[{"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/posts\/168883"}],"collection":[{"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/comments?post=168883"}],"version-history":[{"count":6,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/posts\/168883\/revisions"}],"predecessor-version":[{"id":170519,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/posts\/168883\/revisions\/170519"}],"wp:attachment":[{"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/media?parent=168883"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/categories?post=168883"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/tags?post=168883"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}.animate()<\/code>. He recently wrote about<\/a> how<\/strong> JavaScript animations can be so fast over on David Walsh’s blog, a topic we’ve covered here<\/a> as well. In this article, Julian introduces Velocity.js itself.<\/em><\/p>\n
$.animate()<\/code> 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.<\/p>\n
$.animate()<\/code>‘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.<\/p>\n
$.animate()<\/code>‘s, none of your page’s code needs to change.<\/p>\n
Brief Overview<\/h3>\n
$.animate()<\/code> and
$.velocity()<\/code> support a malleable options syntax. You can either pass in options as comma-separated values, or you can pass in a standalone options object.<\/p>\n
$div.animate(\r\n { \r\n opacity: 1 \r\n }, \r\n 1000, \r\n \"linear\", \r\n function() { \r\n alert(\"Done animating.\"); \r\n }\r\n);<\/code><\/pre>\n
$div.animate(\r\n { \r\n opacity: 1 \r\n }, \r\n { \r\n duration: 1000, \r\n easing: \"linear\", \r\n complete: function() { \r\n alert(\"Done animating!\") \r\n }, \r\n queue: \"myQueue\" \r\n }\r\n);<\/code><\/pre>\n
$div\r\n .animate({ opacity: 1 }, 1000)\r\n .animate({ opacity: 0 }, 1000);<\/code><\/pre>\n
Reversing<\/h3>\n
\"reverse\"<\/code> as its first parameter. Reverse animates the target element toward its values prior to its previous Velocity call.<\/p>\n
$div\r\n \/* Fade an element in while sliding it into view. *\/\r\n .animate({ opacity: 1, top: \"50%\" })\r\n \/* The values below are what we originally set the element to in our stylesheet. Animate back to them. *\/ \r\n .animate({ opacity: 0, top: \"-25%\" });<\/code><\/pre>\n
$div\r\n .velocity({ opacity: 1, top: \"50%\" })\r\n .velocity(\"reverse\");<\/code><\/pre>\n
$div\r\n .velocity({ opacity: 1, top: \"50%\" }, 1000)\r\n \/* Animate back to the prior visual state at half the duration of the previous animation. *\/\r\n .velocity(\"reverse\", 500);<\/code><\/pre>\n
Scrolling<\/h3>\n
scrollTop<\/code> property requires you to target both the
html<\/code> element and the
body<\/code> element in order for the animation to work in older versions of Internet Explorer.<\/p>\n
$(\"html, body\").animate(\r\n { \r\n scrollTop: $div.offset().top \r\n }, \r\n 1000, \r\n function() {\r\n \/* We use a callback to fade in the div once the browser has completed scrolling. *\/\r\n $div.animate({ opacity: 1 });\r\n }\r\n);<\/code><\/pre>\n
$div\r\n .velocity(\"scroll\", 1000)\r\n .velocity({ opacity: 1 });<\/code><\/pre>\n
Looping<\/h3>\n
for (var i = 0; i < 5; i++) {\r\n $div\r\n \/* Slide the element up by 100px. *\/\r\n .animate({ top: -100 })\r\n \/* Then animate back to the original value. *\/\r\n .animate({ top: 0 });\r\n}<\/code><\/pre>\n
$div.velocity(\r\n { top: -100 }, \r\n { loop: 5 }\r\n);<\/code><\/pre>\n
Fading Elements<\/h3>\n
none<\/code>, so that the element wasn’t immediately visible upon page load. Subsequently fading in these elements requires several lines of jQuery:<\/p>\n
$div\r\n \/* Use jQuery's $.show() function to make the element visible by switching its display property to \"block\"\/\"inline\" as appropriate. *\/\r\n .show()\r\n \/* Set the element's starting opacity to 0 so that it can be gradually faded in by the subsequent animation call. *\/\r\n .css(\"opacity\", 0)\r\n \/* Fade in and slide into view. *\/\r\n .animate({ \r\n opacity: 1, \r\n top: \"50%\" \r\n });<\/code><\/pre>\n
$div.velocity(\r\n { \r\n opacity: 1, \r\n top: \"50%\" \r\n },\r\n { \r\n display: \"block\" \r\n }\r\n);<\/code><\/pre>\n
display<\/code> option is set to a value other than
none<\/code>, the element’s
display<\/code> property is set to the provided value at the start<\/em> of the animation. Conversely, when display is passed a value of
none<\/code>, the display property is set upon the animation’s completion<\/em>.<\/p>\n
$div\r\n \/* Fade out and slide out of view. *\/\r\n .animate({ opacity: 0, top: \"-50%\" })\r\n \/* Then set the display property to \"none\" via a queued $.fadeOut() call. *\/\r\n .fadeOut(1);<\/code><\/pre>\n
$div.velocity(\r\n { \r\n opacity: 0,\r\n top: \"-50%\" \r\n }, \r\n { \r\n display: \"none\" \r\n }\r\n);<\/code><\/pre>\n
opacity<\/code> is being animated to a non-zero value while its
display<\/code> option is being set to a value other than
none<\/code>, Velocity conveniently defaults
opacity<\/code>‘s start value to 0.<\/p>\n
Delaying<\/h3>\n
$.delay()<\/code> calls throughout your animation code:<\/p>\n
$div\r\n .delay(1000)\r\n .animate({ \r\n opacity: 1 \r\n });<\/code><\/pre>\n
$div.velocity(\r\n { \r\n opacity: 1 \r\n }, \r\n { \r\n delay: 1000 \r\n }\r\n);<\/code><\/pre>\n
Sequences<\/h3>\n
\n
$.Velocity.Sequences.hover = function (element, options) {\r\n var duration = options.duration || 750;\r\n\r\n $.Velocity.animate(element,\r\n { \r\n translateY: \"-=10px\",\r\n }, { \r\n \/* Delay is relative to user-adjustable duration. *\/\r\n delay: duration * 0.033,\r\n duration: duration,\r\n loop: 3,\r\n easing: \"easeInOutSine\"\r\n });\r\n};<\/code><\/pre>\n
$div.velocity(\"hover\");<\/code><\/pre>\n
fadeIn<\/code>,
fadeOut<\/code>,
slideUp<\/code>, and
slideDown<\/code> functions (which are identical to their jQuery equivalents).<\/p>\n
$div.velocity(\"slideDown\", function() {\r\n \/* Then fade in its children over a duration of 1000ms. *\/\r\n $children.velocity(\"fadeIn\", 1000);\r\n});<\/code><\/pre>\n
Forced Hardware Acceleration<\/h3>\n
translateZ(0)<\/code>.<\/p>\n
$div\r\n .css(\"transform\", \"translateZ(0)\")\r\n .animate({ opacity: 1 })\r\n .css(\"transform\", \"none\");<\/code><\/pre>\n
$div.velocity({ opacity: 1 });<\/code><\/pre>\n
Wrapping Up<\/h3>\n
\n