I would like to speed up my Shopify website load time by lazyloading my images. The problem is that I cannot locate the specific image files in the code in order to add the class= "lazyload" to it.
Here is what I have tried:
I have pasted this code at the top of the product-template.liquid page:
<!--Lazy Loading -->
{{ "lazysizes.min.js" | asset_url | script_tag }}
<style>.lazyload,.lazyloading{opacity:0}.lazyloaded{opacity:1;transition:opacity.3s}</style>
And then adding the code: class= "lazyload" to the end of any chunk of code that I think looks like an image, but it doesn’t work.
Product-template.liquid page 1 page 2 page3
Would anyone be able to help me understand where this code goes and how to identify where I need to put the code to lazyload an image?
2
Answers
I see that you’re using
<img src="..." />
. For lazyload to work, change it to<img data-src="..." />
And the better alternative is to implement native lazy loading. Just add
loading="lazy"
to your images and voila.Ex:
<img src="https://dummyimage.com/600x400/000/fff" loading="lazy" />
More details & a combined use with lazyload library – https://web.dev/native-lazy-loading/ & https://caniuse.com/loading-lazy-attr
In the online code editor, add the below code before close tag.
Add the class lazyload to images that should be lazy loaded. In addition, change the src attribute to data-src.
Instead of