tech @ Beacon Deacon

Image loading - Jamie Johnson

If you have a fast connection, this article may not apply. It will be better seen on a slower connection. And to really see it in action more than once, you will have to clear your cache or do a hard refresh (CTRL+F5 if on Windows). This image loading I demonstrate shows the percentage of load before displaying the images. This can be useful if you have many images or large images, especially in cases where you are going to do something dynamic with the images where the behavior needs all images loaded so it can smoothly execute.

There is a "Loading" progress area showing the percentage of the images loaded. Once all are loaded, then the user is notified that the images are loaded and the images display.

The code

Loading: 0%


The code to do it? Here it is:

<style>#progress { text-align:center; } #progress img { display: none; width: 50%; margin-left: 25%; }</style>
// @JamesArthurJohn
var count = 0;
var pic = 0; 
var speed = 100; 
var quantity = 70; // quantity of pictures 
function preload(){
	mypct = count/quantity;
	mypct = mypct*100;
	mypct = mypct.toString();
	mypct = mypct.split('.');
	mypct = mypct[0];
	mypct = mypct + " %";
	document.getElementById('pct').innerHTML = mypct; 
	if (count == quantity){
		document.getElementById('msg').innerHTML='Images loaded!'; 
<div id="progress"><br /><br /><span id="msg">Loading: </span><span id="pct">0%</span><br /><br />
<img src="yourpicture.jpg" onload="preload()" />
<img src="anotherpicture.jpg" onload="preload()" />
<img src="andanotherpicture.jpg" onload="preload()" />
<img src="onemorepicture.jpg" onload="preload()" />
<div id="override"> </div>

Back to top