Last active
March 16, 2020 14:33
-
-
Save thexmanxyz/391e7870c53ca62e3cd18cbde2d2b57f to your computer and use it in GitHub Desktop.
Lazy load images and remove BS4 spinner dynamically
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var lazyLoadInstance = new LazyLoad({ | |
elements_selector: ".lazy", | |
threshold: 0, | |
callback_loaded: function(el) { $(el).parent().find('.loading-img').remove(); } | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.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