Twitter: raymondcamden


Address: Lafayette, LA, USA

Keep your DOM from shifting around...

08-05-2014 5,140 views Development, jQuery, JavaScript, HTML5 5 Comments

I've been meaning to write this up for a while now, but I never got around to it till today when a meeting got cancelled suddenly. It was this or get on the treadmill, and unfortunately, the treadmill lost. Lately I've noticed a common problem with both web apps and native apps. The problem is this: The application renders some sort of dynamic content. In that content are various UI elements you can click. At the same time, the app is fetching additional content asynchronously. When that content comes in, it is displayed then and the layout of the content is adjusted as the new stuff comes in. The problem is that the user may have been just about to click on a button, link, or whatever, and now finds that their click action has done nothing. Or worse - has activated another action that they didn't want. TweetDeck is especially bad about this. Facebook, surprisingly, is actually pretty darn good about this. Let's look at a simple example in case I'm not making sense.

I've built a simple application that lets you view, and like, pictures of cats. Let me be clear, this is just a proof of concept, but if someone builds this site I'll be hitting that every five minutes or so. When the application loads, you get a picture of the cat, and some, but not all, of the UI.

As you move your mouse, or finger, over the Like button, all of sudden the UI updates to show stats about the picture. The original developer thought it would be cool to load this after the rest of the page. Not a terribly bad idea, right? If the main focus is to show pictures of cats then loading the stats later makes a bit of sense. But see how the DOM changes after the stats were loaded:

As you can see, the Like button was shifted down. In this case the worst you get is a click event that didn't trigger anything, but it is still annoying. You can demo this yourself here: http://www.raymondcamden.com/demos/2014/aug/5/test1.html. Let's quickly look at the code so you can see how the original version was built. First, the HTML.

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<title></title>
		<meta name="description" content="">
		<meta name="viewport" content="width=device-width">
		<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
		<script src="app1.js"></script>
	</head>
	<body>

		<div id="content">
			<img src="http://placekitten.com/300/300">
			<div id="stats"></div>
			<button>Like!</button>
		</div>
		
	</body>
</html>

And here is the JavaScript. I used a simple setTimeout to fake a slow AJAX request.

$(document).ready(function() {

	//fake a delayed update
	window.setTimeout(function() {
		$("#stats").html("<b>Likes:</b> 912");
	},2000);
	
});

Ok, so how can we fix this? One approach may be to simply specify a set height for the DOM item we are updating. That way there won't be a "shift" when the content is uploaded. For example:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<title></title>
		<meta name="description" content="">
		<meta name="viewport" content="width=device-width">
		<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
		<script src="app2.js"></script>
		<style>
			#stats {
				height: 30px;
				background-color: #c0c0c0;
			}
		</style>
	</head>
	<body>

		<div id="content">
			<img src="http://placekitten.com/300/300">
			<div id="stats"></div>
			<button>Like!</button>
		</div>
		
	</body>
</html>

Notice I added both a height and a background-color. The color change was simply to ensure that my height was working right. It also gives the user a bit of a clue that something is going to be there. (I won't pretend this is pretty, but hopefully you get the idea.) You can try this version here: http://www.raymondcamden.com/demos/2014/aug/5/test2.html.

But we can do even better, right? I don't like the big empty box. Let's modify the stats area to include the labels for our stats (well, our stat), so that the update is a bit less jarring. While we're at it, our image service (in this case, the epic placekitten.com) can also be a source of DOM shifting as the image loads. I should have added specific height and width to the image.

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<title></title>
		<meta name="description" content="">
		<meta name="viewport" content="width=device-width">
		<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
		<script src="app3.js"></script>
		<style>
			#stats {
				height: 30px;
			}
		</style>
	</head>
	<body>

		<div id="content">
			<img src="http://placekitten.com/300/300" width="300" height="300">
			<div id="stats">Likes: <span id="likes"></span></div>
			<button>Like!</button>
		</div>
		
	</body>
</html>

I modified the JavaScript now to both add a loading message and to just change the span.

$(document).ready(function() {

	$("#likes").html("<i>Fetching</i>");

	//fake a delayed update
	window.setTimeout(function() {
		$("#likes").html("912");
	},2000);
	
});

You can run this version here: http://www.raymondcamden.com/demos/2014/aug/5/test3.html.

This isn't rocket science, but as I said in the beginning, I find myself surprised by how many sites and apps seem to have this problem. Keep it in mind when working on your next project.

5 Comments

  • Commented on 08-05-2014 at 9:32 AM
    Good stuff Ray.
    It looks like your boiler plate has IE=edge,chrome=1, but I think (at least according to http://www.google.com/chromeframe/about), they've "wound down Chrome Frame and ceased support".
  • Commented on 08-05-2014 at 10:45 AM
    Thanks, I've removed it from my snippet locally.
  • Commented on 08-05-2014 at 11:24 AM
    I think you are definitely right - I just noticed my HTMLLinter complained about it was well.
  • actionless #
    Commented on 08-11-2014 at 2:41 AM
    >>but if someone builds this site I'll be hitting that every five minutes or so

    i know one :-)
    http://www.catsthatlooklikehitler.com
  • Commented on 08-11-2014 at 6:47 AM
    Epic. :)

Post Reply

Please refrain from posting large blocks of code as a comment. Use Pastebin or Gists instead. Text wrapped in asterisks (*) will be bold and text wrapped in underscores (_) will be italicized.

Leave this field empty