I wish to add lazy loading to the images on my webpage however I’ve been having some issues. I’ve been using this guide: (https://speedboostr.com/shopify-lazy-loading/). I’ve done everything correctly so far and some images have been working except for the following code:
<div class="product-item {{ variant.id }}">
<div class="product-item__thumb">
{%- if product.images.size > 1 -%}
<a href="{{ product.url | within: collection }}">
<img class="thumb-primary popup_cart_image""lazyload" src="{{ product.featured_image.src | img_url: 'grande' }}" alt="{{ product.featured_image.alt }}">
{%- for image in product.images limit: 1 offset: 1 -%}
<img class="thumb-secondary popup_cart_image""lazyload" src="{{ image.src | img_url: 'grande' }}" alt="{{ product.featured_image.alt }}">
{%- endfor -%}
</a>
{%- else -%}
<a href="{{ product.url | within: collection }}">
<img class="popup_cart_image""lazyload" src="{{ product.featured_image.src | img_url: 'grande' }}" alt="{{ product.featured_image.alt }}">
</a>
{%- endif -%}
</div>
3
Answers
I would do this via Javascript. Let’s suppose that you have a
function
that receives acontext
and a URL, likeLet’s suppose further that you have an attribute of
img-url="someurl"
wherever you might need to lazyload images later. Then you can do something like this upon page load:However, this is technically not yet a lazy-loading. Something needs to trigger the image loading, but it’s unclear what should trigger it. Above we assume that the page load’s end should trigger the load of all images, but you may need to change this and apply the triggering logic that you prefer.
You should be able to just add the loading="lazy" attribute to the image
You can simply add loading attribute.