What is a good technique for displaying a "Loading ..." image?

I work with div, in which there are any number of elements img. Right now, everyone imghas the following CSS:

#content > img {
display: none;
position: absolute;
top: 0px;
left: 0px;
}

And the images can be cyclic with a function that shows and hides them in turn. However, when loading each image, I would like to display the image "loading ...". I would like to use JavaScript / jQuery to replace the source of each incomplete image for the image "loading ...", still allowing it to be downloaded. What is a common and medium way to do this?

+3
source share
2 answers
$(function(){
   $('<img>').attr('src','loading.gif'); // preload the "loading" image.

   $('#content > img').each(function(){
       var original = this.src;
       var $this = $(this);
       $this.attr('src','loading.gif'); // swap to loading image.
       $('<img>').attr('src',original)// pre-load original.
              .load(function(){
                 $this.attr('src',original); // swap it back when pre-load is done. 
              });
   })
});

crazy example

+5

imagesLoaded, : http://gist.github.com/268257

, imagesLoaded .

UPDATE:

, jQuery : http://api.jquery.com/load-event/

+1

Source: https://habr.com/ru/post/1766131/


All Articles