Skip to content

Instantly share code, notes, and snippets.

@thexmanxyz
Last active March 16, 2020 14:33
Show Gist options
  • Save thexmanxyz/391e7870c53ca62e3cd18cbde2d2b57f to your computer and use it in GitHub Desktop.
Save thexmanxyz/391e7870c53ca62e3cd18cbde2d2b57f to your computer and use it in GitHub Desktop.
Lazy load images and remove BS4 spinner dynamically
<div class="lazy-load-ctn">
<div class="spinner-grow loading-img" role="status">
<span class="sr-only">Loading...</span>
</div>
<img data-src="img/yourimage.png" class="img-fluid lazy">
</div>
var lazyLoadInstance = new LazyLoad({
elements_selector: ".lazy",
threshold: 0,
callback_loaded: function(el) { $(el).parent().find('.loading-img').remove(); }
});
.lazy-load-ctn {
height: 0;
position: relative;
padding-bottom: 28.24%; // use your own percentage to prevent reflow (image width / image height * 100)
overflow: hidden;
img {
position: absolute;
width: 100%;
height: auto;
}
.loading-img {
position: absolute;
margin: auto;
top:0;
bottom: 0;
left: 0;
right: 0;
//color: #448981;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment