@pilot23, So far, not much information is provided accept for "without effect" or "without lazyload effect".
When I add 300 images of 1200x800px to the bottom of page Typography (fresh Grav 1.7.30 installation), none of the images is loaded until scrolling down the page near the end. Hence, lazy loading is working, albeit not yet as efficient as one would like.
default.html.twig:
{% extends 'partials/base.html.twig' %}
{% block content %}
{{ page.content|raw }}
{% for image in page.media.images %}
{{ image.loading('lazy').html()|raw }}
{% endfor %}
{% endblock %}
As said, the browser decides when and how many images to load depending on, for example, the image size, vertical position of images relative to page length, viewport dimensions, scrolling speed, etc.
Using above simple test, about 200+ images are being lazy loaded at once when scrolling down near the end of the page. Why so many? Before loading the images, the browser has no idea about the size of the images.
Do you apply height/width styling rules to the images in css?
- Adding image dimensions in css (or adding height/width directly to the
<img> tag) will give the browser more information to calculate/predict how many images should be loaded on scrolling.
- Adding dimensions will also prevent Cumulative Layout Shift.
When adding the following css to above test, Chrome only loads 1-2 images at a time when scrolling down:
Notes:
-
You should probably add a class to the images using
{{ image.loading('lazy').html('my-title', 'my-alt, 'my-class')|raw }}
and use '.my-class' as selector for the css instead of 'img'.
-
You can have Grav add dimensions automatically to each image by setting the following in file /user/config/system.yaml:
images:
cls: # stands for Cumulative Layout Shift
auto_sizes: true
- But often, you will need to create different image sizes (srcsets) depending on the size of the device and add different dimensions to css depending on the viewport of the device.
-
Also be aware that the gallery being used might interfere with lazy loading.