Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display loading image while actual image is downloading [duplicate]

Tags:

jquery

Sometimes images take some time to render in the browser. I want to show a busy image while the actual image is downloading, and when the image is downloaded, the busy image is removed and the actual image should be shown. How can I do this with JQuery or any javascript?

like image 324
Thomas Avatar asked Jan 08 '11 18:01

Thomas


People also ask

How to display loading image while actual image is downloading?

You can do something like this: // show loading image $('#loader_img'). show(); // main image loaded ? $('#main_img').

How we can load image in background?

Just make a new image via javascript, and only show the image after the onload has fired. This will make sure you don't see any partial rendering of the image. Note that at least chrome will display the "web page loading spinner" in its interface until the image is loaded.

How can we detect an image been fully loaded in the browser?

To determine whether an image has been completely loaded, you can use the HTMLImageElement interface's complete attribute. It returns true if the image has completely loaded and false otherwise. We can use this with naturalWidth or naturalHeight properties, which would return 0 when the image failed to load.

What is image loaded?

The image is considered completely loaded if any of the following are true: Neither the src nor the srcset attribute is specified. The srcset attribute is absent and the src attribute, while specified, is the empty string ( "" ). The image resource has been fully fetched and has been queued for rendering/compositing.


2 Answers

Just add a background image to all images using css:

img {   background: url('loading.gif') no-repeat; } 
like image 77
Gerben Avatar answered Sep 26 '22 06:09

Gerben


You can do something like this:

// show loading image $('#loader_img').show();  // main image loaded ? $('#main_img').on('load', function(){   // hide/remove the loading image   $('#loader_img').hide(); }); 

You assign load event to the image which fires when image has finished loading. Before that, you can show your loader image.

like image 27
Sarfraz Avatar answered Sep 24 '22 06:09

Sarfraz