Lazyload background-image with lazysizes

Daiaiai picture Daiaiai · Nov 8, 2017 · Viewed 20.3k times · Source

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?

Answer

Panos Spiliotis picture Panos Spiliotis · Apr 26, 2018

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>