I want to load images one by one Sequentially from top to bottom e.g. see the attached image.
.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.
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 -->
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With