I want to load a background-image "lazy" with the library http://afarkas.github.io/lazysizes/
They do mention that the loading of "anything" lazily is possible. But the whole documentation on that is that one:
<div data-ride="carousel" data-script="assets/js/bootstrap.min.js" class="carousel lazyload">
<!-- widget content -->
<div>
And I don't get how I could use that for a background-image. Does anybody have experience there on that?
To load a background image using lazysizes use the data-bg
attribute
import "lazysizes/plugins/unveilhooks/ls.unveilhooks";
Usage example:
<div class="lazyload" data-bg="/path/to/image.jpg"></div>
If you want to load background image using lazyload just you need to add one small block of code (No need to add any plugin) -
//add simple support for background images:
document.addEventListener('lazybeforeunveil', function(e){
var bg = e.target.getAttribute('data-bg');
if(bg){
e.target.style.backgroundImage = 'url(' + bg + ')';
}
});
If you want to load an image as background, follow this doc. This is a plugin of lazysizes
Probably someone may find this useful. In my case, using the same library - "lazysizes" I had to change data-bg for data-bgset:
<div class="lazyload" data-bgset=""></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