Reputation: 987
I have a problem with my dynamic integration of Lazysizes plugin. The fact is when I load the page (cache disable), all images of the src are loaded with the content. After this, responsives images are lazy-loaded like I want.
My reflection :
Here my jquery script at the bottom of the page :
//lazy init
window.lazySizesConfig = window.lazySizesConfig || {};
lazySizesConfig.loadMode = 2;
lazySizesConfig.preloadAfterLoad = false;
$('img').addClass('lazyload');
$("img.lazyload").each(function() {
var src = $(this).attr('src').replace(/\.jpg$/i, "");
$(this).attr("data-sizes","auto");
$(this).attr("data-srcset",
src + '-240.jpg 240w,' +
src + '-360.jpg 360w,' +
src + '-480.jpg 480w, ' +
src + '-720.jpg 720w,' +
src + '-768.jpg 768w,' +
src + '-960.jpg 960w,' +
src + '-1280.jpg 1280w'
);
$(this).attr(
"src",
"data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw=="
);
});
I would like only responsive img is load when it is in the viewport. any idea about my issue ?
thanks
Upvotes: 1
Views: 8175
Reputation: 6797
The issue that you're having is caused by the fact, that you're using JavaScript, to set the data-sizes
and data-srcset
attributes.
lazySizes script expects you to specify all of that in your HTML output.
You have to make sure that jQuery will be loaded, and your script will be executed before lazysizes.js
.
I can't guarantee for any of my advice below to be useful, because this script is not intended to be used like that. Still, if you really want to get it to work with jQuery script from your question, consider the following:
async
attribute from lazysizes <script>
tag.lazySizesConfig.preloadAfterLoad = true;
$(document).ready()
.Upvotes: 2
Reputation: 6668
I don't know if lazysizes.js have inside script some document ready function or some DOM change sniffer but I think you need a different approach.
Upvotes: -1