lazySizes is the ultimate and lightweight lazyLoader which lazy loads images (including responsive images (picture/srcset)), iframes and scripts. It is written in VanillaJS and with high performance in mind.
Simply add the JS to your website and put the class
lazyload
to all elements, which should be lazy loaded. For a short API description go to the readme.md.
automatic sizes feature: In case of lazy loading images the sizes
attribute of the img
/source
elements can be calculated with JS.
This automatic sizes feature is directly included in lazySizes. Simply use the keyword auto
inside of the data-sizes
attributes (data-sizes="auto"
).
Important: How sizes
is calculated: The automatic sizes calculation takes the width of the image and the width of its parent element and uses the largest number of those two calculated numbers. It's therefore important, that all images are contained in a wrapper that isn't much bigger than the image should shown. Otherwise a wrong (too big) sizes attribute will be calculated.
Note that you must use either picturefill or respimage (recommended) for full cross browser support of responsive images.
While other lazy loaders or responsive images solution need to be called, if new elements are added to the DOM or do become visible through a special user interaction or a JavaScript behavior.
LazySizes does automatically detect any changes to the DOM and the visibility of .lazyload
elements.