17 min read

In this article by Alex Libby, author of the book Mastering PostCSS for Web Design, you will study about animating elements. A question if you had the choice of three websites: one static, one with badly done animation, and one that has been enhanced with subtle use of animation. Which would you choose? Well, my hope is the answer to that question should be number three: animation can really make a website stand out if done well, or fail miserably if done badly!

So far, our content has been relatively static, save for the use of media queries. It’s time though to take a look at how PostCSS can help make animating content a little easier. We’ll begin with a quick recap on the basics of animation before exploring the route to moving away from pure animation through to SASS and finally across to PostCSS. We will cover a number of topics throughout this article, which will include:

  • A recap on the use of jQuery to animate content
  • Switching to CSS-based animation
  • Exploring the use of prebuilt libraries, such as Animate.css

(For more resources related to this topic, see here.)

Let’s make a start!

Revisiting basic animations

Animation is quickly becoming a king in web development; more and more websites are using animations to help bring life and keep content fresh. If done correctly, they add an extra layer of experience for the end user; if done badly, the website will soon lose more custom than water through a sieve!

Throughout the course of the article, we’ll take a look at making the change from writing standard animation through to using processors, such as SASS, and finally, switching to using PostCSS. I can’t promise you that we’ll be creating complex JavaScript-based demos, such as the Caaaat animation (http://roxik.com/cat/ try resizing the window!), but we will see that using PostCSS is really easy when creating animations for the browser.

To kick off our journey, we’ll start with a quick look at the traditional animation. How many times have you had to use .animate() in jQuery over the years? Thankfully, we have the power of CSS3 to help with simple animations, but there was a time when we had to animate content using jQuery.

As a quick reminder, try running animate.html from the T34 – Basic animation using jQuery animate() folder. It’s not going to set the world on fire, but is a nice reminder of the times gone by, when we didn’t know any better:

If we take a look at a profile of this animation from within a DOM inspector from within a browser, such as Firefox, it would look something like this screenshot:

While the numbers aren’t critical, the key point here are the two dotted green lines and that the results show a high degree of inconsistent activity. This is a good indicator that activity is erratic, with a low frame count, resulting in animations that are jumpy and less than 100% smooth.

The great thing though is that there are options available to help provide smoother animations; we’ll take a brief look at some of the options available before making the change to using PostCSS. For now though, let’s make that first step to moving away from using jQuery, beginning with a look at the options available for reducing dependency on the use of .animate() or jQuery.

Moving away from jQuery

Animating content can be a contentious subject, particularly if jQuery or JavaScript is used. If we were to take a straw poll of 100 people and ask which they used, it is very likely that we would get mixed answers! A key answer of “it depends” is likely to feature at or near the top of the list of responses; many will argue that animating content should be done using CSS, while others will affirm that JavaScript-based solutions still have value.

Leaving this aside, shall we say lively debate? If we’re looking to move away from using jQuery and in particular .animate(), then we have some options available to us:

  • Upgrade your version of jQuery! Yes, this might sound at odds with the theme of this article, but the most recent versions of jQuery introduced the use of requestAnimationFrame, which improved performance, particularly on mobile devices.
  • A quick and dirty route is to use the jQuery Animate Enhanced plugin, available from http://playground.benbarnett.net/jquery-animate-enhanced/ – although a little old, it still serves a useful purpose. It will (where possible) convert .animate() calls into CSS3 equivalents; it isn’t able to convert all, so any that are not converted will remain as .animate() calls.
  • Using the same principle, we can even take advantage of the JavaScript animation library, GSAP. The Greensock team have made available a plugin (from https://greensock.com/jquery-gsap-plugin) that replaces jQuery.animate() with their own GSAP library. The latter is reputed to be 20 times faster than standard jQuery! With a little effort, we can look to rework our existing code. In place of using .animate(), we can add the equivalent CSS3 style(s) into our stylesheet and replace existing calls to .animate() with either .removeClass() or .addClass(), as appropriate.
  • We can switch to using libraries, such as Transit (http://ricostacruz.com/jquery.transit/). It still requires the use of jQuery, but gives better performance than using the standard .animate() command.
  • Another alternative is Velocity JS by Jonathan Shapiro, available from http://julian.com/research/velocity/; this has the benefit of not having jQuery as a dependency. There is even talk of incorporating all or part of the library into jQuery, as a replacement for .animate(). For more details, check out the issue log at https://github.com/jquery/jquery/issues/2053.

Many people automatically assume that CSS animations are faster than JavaScript (or even jQuery). After all, we don’t need to call an external library (jQuery); we can use styles that are already baked into the browser, right? The truth is not as straightforward as this. In short, the right use of either will depend on your requirements and the limits of each method. For example, CSS animations are great for simple state changes, but if sequencing is required, then you may have to resort to using the JavaScript route.

The key, however, is less in the method used, but more in how many frames per second are displayed on the screen. Most people cannot distinguish above 60fps. This produces a very smooth experience. Anything less than around 25FPS will produce blur and occasionally appear jerky – it’s up to us to select the best method available, that produces the most effective solution.

To see the difference in frame rate, take a look at https://frames-per-second.appspot.com/ the animations on this page can be controlled; it’s easy to see why 60FPS produces a superior experience!

So, which route should we take? Well, over the next few pages, we’ll take a brief look at each of these options.

In a nutshell, they are all methods that either improve how animations run or allow us to remove the dependency on .animate(), which we know is not very efficient! True, some of these alternatives still use jQuery, but the key here is that your existing code could be using any or a mix of these methods.

All of the demos over the next few pages were run at the same time as a YouTube video was being run; this was to help simulate a little load and get a more realistic comparison. Running animations under load means less graphics processing power is available, which results in a lower FPS count.

Let’s kick off with a look at our first option—the Transit JS library.

Animating content with Transit.js

In an ideal world, any project we build will have as few dependencies as possible; this applies equally to JavaScript or jQuery-based content as CSS styling.

To help with reducing dependencies, we can use libraries such as TransitJS or Velocity to construct our animations. The key here is to make use of the animations that these libraries create as a basis for applying styles that we can then manipulate using .addClass() or .removeClass(). To see what I mean, let’s explore this concept with a simple demo:

  1. We’ll start by opening up a copy of animate.html. To make it easier, we need to change the reference to square-small from a class to a selector:
    <div id="square-small"></div>
  1. Next, go ahead and add in a reference to the Transit library immediately before the closing </head> tag:
    <script src="js/jquery.transit.min.js"></script>
    
  1. The Transit library uses a slightly different syntax, so go ahead and update the call to .animate() as indicated:
    smallsquare.transition({x: 280}, 'slow');

Save the file and then try previewing the results in a browser. If all is well, we should see no material change in the demo.

But the animation will be significantly smoother—the frame count is higher, at 44.28fps, with less dips.

Let’s compare this with the same profile screenshot taken for revisiting basic animations earlier in this article. Notice anything?

Profiling browser activity can be complex, but there are only two things we need to concern ourselves with here: the fps value and the state of the green line. The fps value, or frames per second, is over three times higher, and for a large part, the green line is more consistent with fewer more short-lived dips.

This means that we have a smoother, more consistent performance; at approximately 44fps, the average frame rate is significantly better than using standard jQuery. But we’re still using jQuery!

There is a difference though. Libraries such as Transit or Velocity convert animations where possible to CSS3 equivalents. If we take a peek under the covers, we can see this in the flesh:

We can use this to our advantage by removing the need to use .animate() and simply use .addClass() or .removeClass().

If you would like to compare our simple animation when using Transit or Velocity, there are examples available in the code download, as demos T35A and T35B, respectively.

To take it to the next step, we can use the Velocity library to create a version of our demo using plain JavaScript. We’ll see how as part of the next demo. Beware though this isn’t an excuse to still use JavaScript; as we’ll see, there is little difference in the frame count!

Animating with plain JavaScript

Many developers are used to working with jQuery. After all, it makes it a cinch to reference just about any element on a page! Sometimes though, it is preferable to work in native JavaScript; this could be for speed. If we only need to support newer browsers (such as IE11 or Edge, and recent versions of Chrome or Firefox), then adding jQuery as a dependency isn’t always necessary.

The beauty about libraries, such as Transit (or Velocity), means that we don’t always have to use jQuery to still achieve the same effect; as we’ll see shortly, removing jQuery can help improve matters! Let’s put this to the test and adapt our earlier demo to work without using jQuery:

    1. We’ll start by extracting a copy of the T35B folder from the code download bundle. Save this to the root of our project area.
    2. Next, we need to edit a copy of animate.html within this folder. Go ahead and remove the link to jQuery and then remove the link to velocity.ui.min.js; we should be left with this in the <head> of our file:
 <link rel="stylesheet" type="text/css" href="css/style.css">
  <script src="js/velocity.min.js"></script>
</head>

    1. A little further down, alter the <script> block as shown:
  <script>
    var smallsquare = document.getElementById('square-small');
    var animbutton = document.getElementById('animation-button');
    animbutton.addEventListener("click", function() {
      Velocity(document.getElementById('square-small'), {left: 280}, {duration: 'slow'});
    });
  </script>
    1. Save the file and then preview the results in a browser. If we monitor performance of our demo using a DOM Inspector, we can see a similar frame rate being recorded in our demo:

With jQuery as a dependency no longer in the picture, we can clearly see that the frame rate is improved—the downside though is that the support is reduced for some browsers, such as IE8 or 9. This may not be an issue for your website; both Microsoft and the jQuery Core Team have announced changes to drop support for IE8-10 and IE8 respectively, which will help encourage users to upgrade to newer browsers.

It has to be said though that while using CSS3 is preferable for speed and keeping our pages as lightweight as possible, using Velocity does provide a raft of extra opportunities that may be of use to your projects. The key here though is to carefully consider if you really do need them or whether CSS3 will suffice and allow you to use PostCSS.

Switching classes using jQuery

At this point, there is one question that comes to mind—what about using class-based animation? By this, I mean dropping any dependency on external animation libraries, and switching to use plain jQuery with either .addClass() or .removeClass() methods.

In theory, it sounds like a great idea—we can remove the need to use .animate() and simply swap classes as needed, right? Well, it’s an improvement, but it is still lower than using a combination of pure JavaScript and switching classes. It will all boil down to a trade-off between using the ease of jQuery to reference elements against pure JavaScript for speed, as follows:

1.      We’ll start by opening a copy of animate.html from the previous exercise. First, go ahead and replace the call to VelocityJS with this line within the <head> of our document:

<script src="js/jquery.min.js"></script>

2.      Next, remove the code between the <script> tags and replace it with this:

var smallsquare = $('.rectangle').find('.square-small');
$('#animation-button').on("click", function() {
      smallsquare.addClass("move");

      smallsquare.one('transitionend', function(e) {
       $('.rectangle').find('.square-small')
       .removeClass("move");
     });
 });

3.      Save the file. If we preview the results in a browser, we should see no apparent change in how the demo appears, but the transition is marginally more performant than using a combination of jQuery and Transit.

The real change in our code though, will be apparent if we take a peek under the covers using a DOM Inspector.

Instead of using .animate(), we are using CSS3 animation styles to move our square-small <div>. Most browsers will accept the use of transition and transform, but it is worth running our code through a process, such as Autocomplete, to ensure we apply the right vendor prefixes to our code.

The beauty about using CSS3 here is that while it might not suit large, complex animations, we can at least begin to incorporate the use of external stylesheets, such as Animate.css, or even use a preprocessor, such as SASS to create our styles.

It’s an easy change to make, so without further ado and as the next step on our journey to using PostCSS, let’s take a look at this in more detail.

If you would like to create custom keyframe-based animations, then take a look at http://cssanimate.com/, which provides a GUI-based interface for designing them and will pipe out the appropriate code when requested!

Making use of prebuilt libraries

Up to this point, all of our animations have had one thing in common; they are individually created and stored within the same stylesheet as other styles for each project.

This will work perfectly well, but we can do better. After all, it’s possible that we may well create animations that others have already built! Over time, we may also build up a series of animations that can form the basis of a library that can be reused for future projects.

A number of developers have already done this. One example of note is the Animate.css library created by Dan Eden. In the meantime, let’s run through a quick demo of how it works as a precursor to working with it in PostCSS.

The images used in this demo are referenced directly from the LoremPixem website as placeholder images.

Let’s make a start:

      1. We’ll start by extracting a copy of the T37 folder from the code download bundle. Save the folder to our project area.
      2. Next, open a new file and add the following code:
        body { background: #eee; }
        
        #gallery {
          width: 745px;
          height: 500px;
          margin-left: auto;
          margin-right: auto;
        }
        
         
        
        #gallery img {
          border: 0.25rem solid #fff;
          margin: 20px;
          box-shadow: 0.25rem 0.25rem 0.3125rem #999;
          float: left;
        }
        
        .animated {
          animation-duration: 1s;
          animation-fill-mode: both;
        }
        
        .animated:hover {
          animation-duration: 1s;
          animation-fill-mode: both;
        }
      1.  Save this as style.css in the css subfolder within the T37 folder.
      2. Go ahead and preview the results in a browser. If all is well, then we should see something akin to this screenshot:

If we run the demo, we should see images run through different types of animation; there is nothing special or complicated here. The question is though, how does it all fit in with PostCSS?

Well, there’s a good reason for this; there will be some developers who have used Animate.css in the past and will be familiar with how it works; we will also be using a the postcss-animation plugin later in Updating code to use PostCSS, which is based on the Animate.css stylesheet library. For those of you who are not familiar with the stylesheet library though, let’s quickly run through how it works within the context of our demo.

Dissecting the code to our demo

The effects used in our demo are quite striking. Indeed, one might be forgiven for thinking that they required a lot of complex JavaScript!

This, however, could not be further from the truth. The Animate.css file contains a number of animations based on @keyframe similar to this:

@keyframes bounce {
  0%, 20%, 50%, 80%, 100% {transform: translateY(0);}
  40% {transform: translateY(-1.875rem);}
  60% {transform: translateY(-0.9375rem);}
}

We pull in the animations using the usual call to the library within the <head> section of our code. We can then call any animation by name from within our code:

  <div id="gallery">
    <a href="#"><img class="animated bounce" src="http://lorempixum.com/200/200/city/1" alt="" /></a>
...
  </div>
  </body>

You will notice the addition of the .animated class in our code. This controls the duration and timing of the animation, which are set according to which animation name has been added to the code.

The downside of not using JavaScript (or jQuery for that matter) means that the animation will only run once when the demo is loaded; we can set it to run continuously by adding the .infinite class to the element being animated (this is part of the Animate library). We can fake a click option in CSS, but it is an experimental hack that is not supported across all the browsers. To affect any form of control, we really need to use JavaScript (or even jQuery)!

If you are interested in details of the hack, then take a look at this response on Stack Overflow at http://stackoverflow.com/questions/13630229/can-i-have-an-onclick-effect-in-css/32721572#32721572.

Okay! Onward we go. We’ve covered the basic use of prebuilt libraries, such as Animate. It’s time to step up a gear and make the transition to PostCSS.

Summary

In this article, we studied about recap on the use of jQuery to animate content. We also looked into switching to CSS-based animation. At last, we saw how to make use of prebuilt libraries in short. 

Resources for Article:

 


Further resources on this subject:


LEAVE A REPLY

Please enter your comment!
Please enter your name here