smart-image-preloader
v1.0.1
Published
A lightweight module for lazy loading and preloading images with priorities and animations
Maintainers
Readme
smart-image-preloader
Lightweight helper for lazy-loading images with simple transitions and automatic WebP detection.
Description
The preloadImages function observes images in the DOM (by selector) and loads them when they enter the viewport. It supports priority ordering, simple transitions (fade, slide-in, scale), automatic WebP selection, and a fallback for browsers without IntersectionObserver.
Installation
npm install smart-image-preloaderFor local development:
npm install
# then import from `src/index.js` or build with your bundlerBasic usage
Import and initialize the preloader for a selector of images:
import { preloadImages } from 'smart-image-preloader';
preloadImages('.lazy-img');Expected HTML
Add data-src (and optionally data-src-webp and data-priority) to your img elements:
<img class="lazy-img" data-src="/images/photo.jpg" data-src-webp="/images/photo.webp" data-priority="high" alt="...">API
preloadImages(selector, options = {})
Available options:
priority(string) — Default:'low'. Used when an image does not havedata-priority. Images withdata-priority="high"are ordered first.transition(string) —'fade'(default),'slide-in','scale'or'none'. Controls the animation applied when the image appears.onImageLoaded(function) — Callback invoked with theimgelement when an image finishes loading.onAllImagesLoaded(function) — Callback invoked when all images have been loaded.
Return: none. The module also dispatches a DOM custom event image-loaded on the img element when it finishes loading.
Events and callbacks
You can listen for the image-loaded custom event on each image:
document.querySelectorAll('.lazy-img').forEach(img => {
img.addEventListener('image-loaded', (e) => {
const loadedImg = e.detail.img;
// do something with loadedImg
});
});Or use the function callbacks:
preloadImages('.lazy-img', {
transition: 'slide-in',
onImageLoaded: (img) => console.log('loaded', img.src),
onAllImagesLoaded: () => console.log('all images loaded'),
});Support and fallback
- Detects WebP support at runtime and uses
data-src-webpwhen available. - Uses
IntersectionObserverwhen present for efficiency. If not available, falls back to ascroll/resizebased check usinggetBoundingClientRect.
Edge cases and notes
- If no images match the selector the function logs a
console.warnand exits gracefully. - Ensure your
imgelements start with a placeholder (for example a tiny inline image orsrc="data:image/gif;base64,...") or nosrcuntil the real source is loaded.
Contributing
If you'd like to contribute, open an issue or a pull request. Keep PRs small and add tests for functional changes.
