jquery - image loading

jquery RSS Icon ATOM Icon 2011/02/24 22:01 visualp

[원문]   http://jqueryfordesigners.com/image-loading/

Basic Version

In our basic version, we will have a single div containing 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:

  1. Use a background image on the holder div, this way we can easily centre align horizontally and vertically using CSS, rather than adding extra markup.
  2. Adding a styled div in our holder div, 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:

<div id="loader" class="loading"></div>


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.

DIV#loader {
: 1px solid #ccc;
: 500px;
: 500px;

 * While we're having the loading class set.
 * Removig it, will remove the loading message

#loader.loading {
: url(images/spinner.gif) no-repeat center center;


The jQuery’s job is to:

  1. Load the image in the background
  2. Hook a load event
  3. Once the image has loaded, strip the loading class and insert the image
// when the DOM is ready
(function () {
var img = new Image();
// wrap our new image in jQuery, then:
// once the image has loaded, execute this code
.load(function () {
// set the image hidden by default    
// with the holding div #loader, apply:
// remove the loading class (so no background spinner),
// then insert our image
// fade our image in to create a nice effect
// if there was an error loading the image, react accordingly
.error(function () {
// notify the user that the image could not be loaded
// *finally*, set the src attribute of the new image to our image
.attr('src', 'images/headshot.jpg');

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 map linked to this image that you wanted to request via Ajax the contents of the load function would be this:

.load(function () {
// our bespoke ajax hit that's required with the image
// it will return the HTML for the <map> element and
// linked <area> element.
: 'image-map.php',
: 'img=' + i.src, // the image url links up in our fake database
: 'html',
: function (html) {
// because we're inside of the success function, we must refer
// to the image as 'img' (defined originally), rather than 'this'
// now show the image
// code continues as before

Related screencasts

2011/02/24 22:01 2011/02/24 22:01
받은 트랙백이 없고, 댓글이 없습니다.

댓글+트랙백 RSS :: http://blog.visualp.com/rss/response/360

댓글+트랙백 ATOM :: http://blog.visualp.com/atom/response/360

트랙백 주소 :: 이 글에는 트랙백을 보낼 수 없습니다

트랙백 RSS :: http://blog.visualp.com/rss/trackback/360

트랙백 ATOM :: http://blog.visualp.com/atom/trackback/360