Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load images in order, with delay, fallback for no script

I'm wondering if anyone has a nice clean solution to loading images from top to bottom of a page, waiting for the previous image to load before going to the next. And in case the user dosent have javascript fall back on a regular <img> tag.

There are quite a few lazy loading plugins, but I would like to load all images as fast as possible in the order they appear on the website.

The reason for this is that each image is rather large, and the user will look through them from top to bottom in a rather slow fashion.

like image 525
JensB Avatar asked Dec 08 '25 21:12

JensB


1 Answers

interesting question. my approach would be something like this

$(function(){

  var loadNext = function(){
    var next_guy = $('#imgz img[x-src]').first();
    next_guy.prop('src', next_guy.attr('x-src'));
    next_guy.removeAttr('x-src');
    
  };
  
  $('#imgz img').on('load',loadNext);
  
});
#imgz img {
  width: 250px;
  border: 1px dotted gray;
  clear: both;
  display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="imgz">
  <img src="http://i.imgur.com/mThaO.jpg" />
  <img x-src="http://i.imgur.com/DO1kZ.jpg" />
  <img x-src="http://i.imgur.com/lD2HS.jpg" />
  <img x-src="http://i.imgur.com/4vqE3.jpg" />
  <img x-src="http://i.imgur.com/TXXbx.jpg" />
  <img x-src="http://i.imgur.com/TF3z2.jpg" />
</div>
like image 138
code_monk Avatar answered Dec 11 '25 11:12

code_monk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!