简体   繁体   中英

how to load images after everything else is loaded?

i got some idea that to load a fake image on a div before original image finishies loading and that to be after winodws load.i got some code but cant understand in which part i have to put src image for fake image and original image.do help me out in this code . my html part is

<div class="myimage"><img src="myimage.jpg" /></div>

note:Trick is to set the src attribute only when that source is loaded in temporary img. $(img).load(fn); img. $(img).load(fn); handles that.

$(function(){
        $.each(document.images, function(){
                   var this_image = this;
                   var src = $(this_image).attr('src') || '' ;
                   if(!src.length > 0){
                       //this_image.src = options.loading; // show loading........
                       var lsrc = $(this_image).attr('lsrc') || '' ;
                       if(lsrc.length > 0){
                           var img = new Image();
                           img.src = lsrc;
                           $(img).load(function() {
                               this_image.src = this.src;
                           });
                       }
                   }
               });
      });

You can use data attribute to do it:

<div class="myimage"><img data-orig="original.jpg" src="fake.jpg" /></div>

$(window).load(function(){
  $('.myimage img').attr("src", $(this).data('orig')).removeAttr('data-orig');
});

You can add your images url to a custom data-url attribute and make your image url a base64 1x1 gif. And then when the page loads completely you can get your url from it. Basically it will be like this.

<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-src="original-image.png" />

$(window).load(function() {

   $("img").each(function() {
     $(this).attr("src", $(this).attr("data-src"));
   });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM