DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • Visually Designing Views for Java Web Apps
  • Step-by-Step Guide: Application Using NestJs and Angular
  • Top 10 PHP Development Tools For Efficient PHP Developers
  • Designing Web Apps for High Availability in AWS

Trending

  • Log Analysis Using grep
  • Decoding Business Source Licensing: A New Software Licensing Model
  • Chronicle Services: Low Latency Java Microservices Without Pain
  • Hugging Face Is the New GitHub for LLMs
  1. DZone
  2. Coding
  3. Languages
  4. How to Keep Your DOM from Shifting Around

How to Keep Your DOM from Shifting Around

Raymond Camden user avatar by
Raymond Camden
·
Sep. 02, 14 · Interview
Like (0)
Save
Tweet
Share
3.44K Views

Join the DZone community and get the full member experience.

Join For Free

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.

app Statistics application Web apps JavaScript Concept (generic programming) Web Service HTML Requests

Published at DZone with permission of Raymond Camden, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Visually Designing Views for Java Web Apps
  • Step-by-Step Guide: Application Using NestJs and Angular
  • Top 10 PHP Development Tools For Efficient PHP Developers
  • Designing Web Apps for High Availability in AWS

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: