Animate a site’s loading

Note: This article has been marked for a quality review and will soon be updated.

There are a lot of things we can do as web designers to improve the UX of a site, much of the time subtle animations can be added to add a bit of finesse to a page, and a new trend is that of adding animations to the loading of pages to make it look a bit better. So what do I mean by ‘loading animations’? I’m not talking about the annoying “feature” of having a site load in a flash and then display a loading spinner while it actually pulls up the content using AJAX; rather adding a tiny amount of code to gain more control over how our site loads. But before we jump in to the code we need to remember a couple of things: first the animations can’t take very long, after all we’re trying to improve the user experience, so having a site take 5 seconds to load in will just frustrate the user. And second, as we will be using JavaScript for this, we need to add a fallback in case it’s turned off.

Here’s what we’ll be creating (new window).

So to begin I’ve written up a very simple page, here’s the HTML:

	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	<title>Animated Loading</title>
	<!--[if IE]>
		<script src=""></script>
	<link rel="stylesheet" href="style.css" type="text/css" />
	<div class="wrapper">
			<h1 class="left">My Website</h1>
		<section class="hidden">
			<p>Yay for loading animations!</p>
		<footer class="hidden">
			<p>Copyright goes here</p>

Notice the use of the class “hidden” – that does exactly what you might think, and to make sure it actually works, let’s hook up the CSS:

/* Globals */
.wrapper {
	display: block;
	margin:0 auto;
	padding:0 10px;

.hidden {
	display: none;

.left { float:left; }
.right { float:right; }

body {
	background: #dfdfdf;

/* Header */
header {
	overflow: hidden;
	position: relative;

h1 {
	background: #222;
	font-size: 2.5em;
	line-height: 0.4em;
	color: #fff;
nav {
	float: right;

	nav li {
		float: left;
		text-align: center;
		cursor: pointer;
		display: none;
/* Sections */
section {
	background: #fff;
	overflow: hidden;
	section h2 {
		font-size: 1.25em;
		font-weight: bold;

/* Footer */
footer {
	text-align: center;

Again, some very basic CSS to ensure it actually looks like a functional website. Notice how elements such as the ‘nav li’ elements are hidden using CSS (you might want to just add a class in the HTML, but if they are dynamically generated, this is your best bet).

If we open the page now we don’t see very much, some would say it was blank, many people actually. This is what users without JavaScript enabled will see. So to remedy that we need to make use of the <noscript> tag in our header. Just before the closing <head> tag, add the following code:

	<style type="text/css">
		header {
			top:0px !important;
		section, footer, nav li {
			display: block !important;

That code will only be executed if we don’t have access to JavaScript, and so will force the elements we worked so hard to hide, to show up. Good stuff, we’re not breaking the internet by requiring JavaScript! And on that note, let’s write some.

Here’s the code I wrote for the example – it’s very basic, and executes within 2.45 seconds (but because some animations are in parallel it feels like 1.25 seconds). Make sure you include jQuery on your page, and then whack in the following code:

	/* Loading Animations */
	$('header').animate({'top':0},500, function(){
		$('nav li').each(function(index){
		$('section, footer').fadeIn(750).removeClass('hidden');

		$('section, footer').fadeOut(300);

The first part of the that code waits for the DOM to be fully loaded, and then animates the header to slide in. When the header has finished showing up we cycle through the navigation’s list elements, sliding them down at a growing animation length (notice the ‘+1’ to ensure we don’t multiply by zero). Once that’s done we fade in any other elements we had hidden, and remove the class of ‘hidden’ – here you could use another each() cycle, but in the interests of speed I just stuck with a simple fade in effect. The next block of code is for links away from the page. We look for any links, and then whenever they are clicked, we quickly animate the contents of the page out in 0.3 seconds – I think this is just a nice touch. With a bit of tweaking of things like timings and the odd animation, you can make a visitor smile before they’ve even read what’s on your site.

And that’s how to improve the UX of your websites with subtle loading animations.





One response to “Animate a site’s loading”

  1. James Waples avatar

    I’ve wanted to do something similar to this for a long while now, but have always been stumped by the “no JavaScript” problem. Why didn’t I think of the solution before! Great work, and I love your comment form 🙂