Creating a Loading Spinner Animation in CSS and JavaScript

Update (12/20/2011): Now works in Firefox as well as Chrome and Safari.

Since I’m not a very good designer, I usually try to do as much styling, design, and graphics in code as I can. For instance, when I wrote this mobile compass application in HTML, I did all the graphics programmatically using Canvas and CSS.

I’m now working on a project that requires one of those loading spinner animations that Apple seems to have made famous, so naturally, I started looking into ways to create one purely in code and/or CSS (no external assets). Fortunately, I found a great post on the Signal vs. Noise blog demonstrating exactly what I wanted to do. However, I decided to take it one step further, and write some JavaScript to generate both the required CSS and the HTML. The advantage of generating everything dynamically is that you can configure things like the size, color, and the number of bars in the animation at runtime.

Here’s an example of some randomly generated loading spinner animations which are 100% CSS and HTML generated by JavaScript (Chrome and Safari only for now). If you’re interested in the code, here’s the original CSS from Signal vs. Noise, and below is my port to JavaScript (view the source of the demo to see how to use it). It only works in WebKit-based browsers for the time being, but I’ll update it as browser capabilities get better. It works in WebKit-based browser, and in Firefox.

Check out the example.

var Spinner = {
  SPINNER_ID: '_spinner',
  prefix: (navigator.userAgent.indexOf('WebKit') != -1) ? 'webkit' : 'moz',
  getSpinner: function(size, numberOfBars, color) {
    if (document.getElementById(this.SPINNER_ID) == null) {
      var style = document.createElement('style');
      style.setAttribute('id', this.SPINNER_ID);
      style.innerHTML = '@-'+this.prefix+'-keyframes fade {from {opacity: 1;} to {opacity: 0.25;}}';
    var spinner = document.createElement('div'); = size; = size; = 'relative';
    var rotation = 0;
    var rotateBy = 360 / numberOfBars;
    var animationDelay = 0;
    var frameRate = 1 / numberOfBars;
    for (var i = 0; i < numberOfBars; ++i) {
      var bar = document.createElement('div');
      spinner.appendChild(bar); = '12%'; = '26%'; = color; = 'absolute'; = '44.5%'; = '37%'; = '1';'-'+this.prefix+'-border-radius', '50px', null);'-'+this.prefix+'-box-shadow', '0 0 3px rgba(0,0,0,0.2)', null);'-'+this.prefix+'-animation', 'fade 1s linear infinite', null);'-'+this.prefix+'-transform', 'rotate('+rotation+'deg) translate(0, -142%)', null);'-'+this.prefix+'-animation-delay', animationDelay + 's', null);
      rotation += rotateBy;
      animationDelay -= frameRate;
    return spinner;

7 Responses to Creating a Loading Spinner Animation in CSS and JavaScript

  1. Pingback: xhtml css templates – Creating a Loading Spinner Animation in CSS and JavaScript … | XHTML CSS - Style sheet and html programming tutorial and guides

  2. It would be interesting to see it done via pure CSS, using just CSS transforms and animations.

    • Christian Cantrell says:

      That’s actually what’s happening. It’s 100% CSS using animations, but the elements are just created using JS for two reasons: 1. To fit better into the way I like to work; 2. So their properties can be dynamic. But what gets generated is all just HTML and CSS.

  3. Jquery rocks……. Great Post

  4. tatata says:

    A good example!

  5. Randy Edmunds says:

    Very cool!

  6. sathyamoorthi says:

    Great post. But as usual it is not working in IE 9.