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 if it is over 40
(see also minSize
option). In case it's below the minSize
threshold, it traverses up the DOM tree until it finds a parent which is over 40
and uses this number. Often the following general CSS rule might help: img[data-sizes="auto"] { display: block; }
.
For responsive images support you must use either use a full polyfill like picturefill or respimage or use the extreme lightweight partial respimg polyfill plugin or use the Responsive Images as a Service extension.
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.