Image Loading
Posted on 4th February 2008 — Daniel Mee requested a tutorial and writes:
I have a large image (500k). I have a loading gif (little rotating circle thingy from ajaxload.info). I want the animated gif to be swapped with the large image once it’s loaded. The event may be on page load or may be some button onClick…
This is similar to how a LightBox would work, except Daniel wants complete control of the the load event.
This tutorial will show how to load images in the background, and once loaded handle the event and create your own response.

Basic Version
In our basic version, we will have a single
divcontaining a loading spinner and once our large image is loaded (in the background) we will remove the spinner and insert our image.There’s a few ways to approach the loading screen, two of which are:
div, this way we can easily centre align horizontally and vertically using CSS, rather than adding extra markup.divin our holderdiv, then remove the entire block of markup when the image loads.I’ve provided a screencast explaining how to achieve this (though it is based on the CSS version, also shows how to do this with a separate loading
div).Watch the jQuery basic image load screencast (alternative flash version)
View the demo and source code used in the screencast
Note that in the demonstration as I am simulating loading a slow to load image by including a script tag at the bottom of the markup. In your real world version, you obviously would not include it.
HTML Markup
The markup (segment) that we’re using is extremely simple. Note, however, I’ve included a ‘loading’ class on the
div. This will be manipulated in our jQuery once the image has loaded behind the scenes:CSS
For the demo, I’ve set the height and width of the empty
div, but also included a background image which indicates something is loading.You can use any kind of image – I’ve seen applications use much larger animated gifs to simulate a loading message box.
jQuery
The jQuery’s job is to:
Added Functionality
The jQuery code used is the constructs of our load function. If we wanted to style the image in any way, add classes or run an Ajax request before showing the image, it would go inside the
.load()function.This functionality could also be placed inside of a
.click()event handler or anything else if you wanted to trigger the image loading.For example, if you had a particular image
maplinked to this image that you wanted to request via Ajax the contents of the load function would be this:You should follow me on Twitter here I tweet about jQuery amongst the usual tweet-splurges!