lazyLoadImage — plain JavaScript library, the simplest way to load pictures assets on page asynchronously.
Features:
- Async loading for
<img>tags. - Async loading
background-imageforcss. - Start loading when element's boundary is in viewport.
- Support updating tracking elements list when changing DOM happens.
- Support element's boundary offset for start loading before element will be in viewport.
- Support binding element's boundary to its parent.
All elements with
data-srcattribute will be added to lazyLoadImage tracking list automatically after starting lazyLoadImage tracking. It works withMutationObserver, so it tracks when new elements will be created or some elements will be removed.
You don't need start tracking more than one time after script import.
- Download and import lazyLoadImage to page with:
<script type="text/javascript" src="lazyLoadImage.js"></script>. - Then start lazyLoadImage tracking with executing:
lazyLoadImage.start(). - To make element visible for lazyLoadImage, add
data-srcattribute to element instead ofsrcorbackground-image. - To use all features, check options below.
To control way for loading items you should set some attributes.
- Default attribute for indicating to track and handle element with lazyLoadImage, all items with only this attribute will start loading after
DOMContentLoadedevent. - By default this attribute will start load item like
<img>tag withsrcattribute, to change it → checkdata-cssattribute below.
- Attribute that will start load element when its boundary box (according to Y axis) will be in the user viewport.
- If you want to use parent element boundary box → to check if is in viewport, then set
data-viewportattribute to value:parent.
-
Attribute that add offset for element boundary box (when
data-viewportis used). -
You should use it if wanna make element loading start before will be in viewport.
-
To make element loading start when element will be in a viewport after 400px of page scroll, set value:
-400.
- Attribute that changes method of loading to →
background-imageforcss.
You can check source of example using all features of lazyLoadImage in example directory → example page.
- Upload library to public.
- Add support execution of
viewportChangefor window resize event. - Check element boundary box with
data-offsetwhen it's only visible when some conditions (css) anddata-viewportenabled. - Add SVG elements async loading support.