I am trying to lazyload a background image. In order to do this I added the lazy load plugin of jquery: jquery-lazy. We install it with npm i jquery-lazy
.
In order to have lazyloaded images I just need to add the class lazy like below:
<img data-src="image.png" class="lazy">
with the js below:
<script>
$(function($) {
$("img.lazy").Lazy();
});
</script>
And it is working correctly.
Unfortunately, I don't know how to do for background images like that:
<div style="background-image: url(background.jpg)">
Indeed here the background image is added in a <div>
in the style field...
I am wondering if I can lazyload a background image the same way I did with basic images like <img>
.
I tried to do it by adding <div class="lazy">
but it is not working.