Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to load the images sequentially from top to bottom in webpage?

I want to load images one by one Sequentially from top to bottom e.g. see the attached image.

enter image description here

  • First load: Image 01
  • When completely loaded image 01 then load image 02,
  • When completely loaded image 02 then load image 03 and so on...

.my-container {
  width: 1060px;
  margin: 0 auto;
  padding: 50px;
}
img {
  margin: 0 auto 35px;
  display: block;
  width: 100%;
  max-width: 100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="my-container">
  <div class="portfolio">
    <img alt="image 01" width="960" height="640" src="http://i.imgur.com/wIfbFMu.jpg">
    <img alt="image 02" width="960" height="640" src="http://i.imgur.com/thiOajQ.jpg">
    <img alt="image 03" width="960" height="720" src="http://i.imgur.com/YgIxbVF.jpg">
    <img alt="image 04" width="960" height="640" src="http://i.imgur.com/Xt6R20O.jpg">
    <img alt="image 05" width="960" height="625" src="http://i.imgur.com/XENpTvq.jpg">
    <img alt="image 06" width="960" height="638" src="http://i.imgur.com/qBMOAgZ.jpg">
  </div>
</div><!-- .my-container -->

I preferred jQuery solution.

like image 909
Usman Arshad Avatar asked Jan 05 '23 03:01

Usman Arshad


2 Answers

Set up a function that loads the image at the current index, and increments the index when the image has loaded.

Using the data-src attribute makes it so that the image does not load until the URL is put into the src attribute.

I've added a console log to show that the images are loading sequentially.

$(function () {
   var $images = $('.portfolio img');
   var lastLoadIndex = 0;
   var loadNextImage = function () {
      if ($images.length === lastLoadIndex) {
          return;
      }
console.log('loading image at index ' + lastLoadIndex);
      $images.eq(lastLoadIndex).attr('src', $images.eq(lastLoadIndex).attr('data-src'));
      lastLoadIndex += 1;
   };
   $images.on('load', loadNextImage);
   loadNextImage();
});
.my-container {
  width: 1060px;
  margin: 0 auto;
  padding: 50px;
}
img {
  margin: 0 auto 35px;
  display: block;
  width: 100%;
  max-width: 100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="my-container">
  <div class="portfolio">
    <img alt="image 01" width="960" height="640" data-src="http://i.imgur.com/wIfbFMu.jpg">
    <img alt="image 02" width="960" height="640" data-src="http://i.imgur.com/thiOajQ.jpg">
    <img alt="image 03" width="960" height="720" data-src="http://i.imgur.com/YgIxbVF.jpg">
    <img alt="image 04" width="960" height="640" data-src="http://i.imgur.com/Xt6R20O.jpg">
    <img alt="image 05" width="960" height="625" data-src="http://i.imgur.com/XENpTvq.jpg">
    <img alt="image 06" width="960" height="638" data-src="http://i.imgur.com/qBMOAgZ.jpg">
  </div>
</div><!-- .my-container -->
like image 168
Heretic Monkey Avatar answered Jan 06 '23 21:01

Heretic Monkey


Vanilla JS solution.

    function init() {
      var imgDefer = document.querySelectorAll(".img-sequence");
      var lastLoadIndex = 0;
      function loadNextImage() {
        if (imgDefer.length === lastLoadIndex) {
          return;
        }
        imgDefer[lastLoadIndex].addEventListener("load", loadNextImage);
        imgDefer[lastLoadIndex].setAttribute(
          "src",
          imgDefer[lastLoadIndex].getAttribute("data-src")
        );
        lastLoadIndex += 1;
      };
      loadNextImage();
    }
    window.onload = init;
<div>
	<img class="img-sequence" data-src="https://picsum.photos/320/320?image=0" />
	<img class="img-sequence" data-src="https://picsum.photos/320/320?image=1" />
	<img class="img-sequence" data-src="https://picsum.photos/320/320?image=2" />
	<img class="img-sequence" data-src="https://picsum.photos/320/320?image=3" />
	<img class="img-sequence" data-src="https://picsum.photos/320/320?image=4" />
	<img class="img-sequence" data-src="https://picsum.photos/320/320?image=5" />
	<img class="img-sequence" data-src="https://picsum.photos/320/320?image=6" />
	<img class="img-sequence" data-src="https://picsum.photos/320/320?image=7" />
</div>
like image 45
rgfx Avatar answered Jan 06 '23 20:01

rgfx