views:

583

answers:

5

Hi all,

How to show loading image when a big image is being loaded?

As an example in Orkut when viewing a photo in user photo album there is a loading image shown over the photo until the Photo is completely loaded.

I need to implement that feature.

My question is how implement that feature? Is it possible without using JQuery?

Please help.

+12  A: 

Wrap your image in a div (or whatever you want) and set it's background image to be an animated gif or whatever loading image you want. When the image finishes loading it will cover up the background image. Easy and can be reused wherever you want.

<div class="loading">
    <img src="bigimage.jpg" alt="test" />
</div>

CSS:

.loading{
    background:transparent url(loadinggif.gif) center center no-repeat;
}
NinjaBomb
The wrapper `div` isn't necessary. You can apply the background to the image. Also, if the image is transparent, the loading image will be visible underneath
Justin Johnson
Is there any other way out using asp.net ajax or simple javascript nto JQuery. Please help. Thaks for your reply.
Himadri
@Himadri - why do you want to make it more complicated?
Skilldrick
Justin - You are correct, it isn't necessary. Just wanted to give a simple example.
NinjaBomb
@Himadri: The approach here uses pure CSS which you can include on the page. You can even add those style rules directly to the image object (though I recommend using a the above approach instead): <img style=" background: transparent url(loadinggif.gif) center center no-repeat;" src="bigimage.jpg">
Mr. Shiny and New
You could do this using JavaScript with or without jQuery... but unless there's a compelling reason, I would probably use something similar to this answer
Akrikos
+1  A: 

You could use the jQuery Lazy Loading plugin. It allows you to specify a loading image and delays the loading of large images until they are scrolled into view.

AJ
Is there any other way out using asp.net ajax or javascript instead of JQuery?Thanks for ur post.
Himadri
A: 

Edit: Apparently, this has been deprecated. Nothing to see here, move along.


No JavaScript or CSS is necessary for this. Just use the built-in, but seldom heard-of, lowsrc property for img elements.

<img src="giant-image.jpg" lowsrc="giant-image-lowsrc.gif">

The basic idea is that you create an additional very compressed, possibly black and white version of your normal image. It gets loaded first and when the full resolution image is downloaded, the browser replaces it automatically. The best part is you don't have to do anything.

Check it out here: http://www.htmlcodetutorial.com/images/_IMG_LOWSRC.html

Justin Johnson
I wish I could give this more upvotes.
eyelidlessness
I was fascinated by this answer since I've never heard of 'lowsrc' property before. I did a google search and came across this: http://blog.throbs.net/2009/05/27/Hey+JLo+Where+Did+LowSRC+Go.aspx It seems to state that 'lowsrc' is deprecated for a while. The only sources/pages I found for 'lowsrc' are very very old. I was also not able to use any of the pages to test (and actually see the lowsrc in action). Maybe my connection is too fast.
Jim
From my searching online, it seems like this property can been hacked back in by using javascript. If you run html source with image lowsrc attribute through http://validator.w3.org/ it will show that 'lowsrc' is not a valid attribute.
Jim
Well blow me down. I've never used it myself, just heard of it.
Justin Johnson
I got the html error 'lowsrc' is not a valid attribute of element 'img' and can't see any difference using it.
Himadri
@himadri ya, that's what @jim said
Justin Johnson
Too bad it's deprecated, it would be a simple solution for something that is sometimes still needed.
Pekka
+3  A: 

Here's a basic technique that you can expand upon to do more elaborate stuff with

<html>
<head>
  <title>Test Page</title>
  <script type="text/javascript">
    onload = function()
    {
      // Create an image object. This serves as a pre-loader
      var newImg = new Image();

      // When the image is given a new source, apply it to a DOM image
      // after it has loaded
      newImg.onload = function()
      {
        document.getElementById( 'test' ).src = newImg.src; 
      }

      // Set the source to a really big image
      newImg.src = "http://apod.nasa.gov/apod/image/0710/iapetus2_cassini_big.jpg";    
    }
  </script>
</head>

<body>

<img id="test" src="http://upload.wikimedia.org/wikipedia/commons/4/42/Loading.gif" >

</body>
</html>
Peter Bailey
A: 

Time ago I made something like this for a similar problem:

<script>
    function imageLoaded(img) {
        document.getElementById('loadingImage').style.visibility='hidden';
        img.style.visibility='visible';
    }
</script>
...
<img id='loadingImage' src='loading.gif'/>
<img src='bigImage.jpg' style='visibility:hidden;' onload='javascript:imageLoaded(this);'/>

I think this approach has some useful advantages:

  • The loading image is hidden. Imagine your big image isn't so big as you expected...
  • You are able to do some extra things in javascript function. In my case, I stretched image width, height or both, depending on its size.
Sinuhe