npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

smart-image-preloader

v1.0.1

Published

A lightweight module for lazy loading and preloading images with priorities and animations

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-preloader

For local development:

npm install
# then import from `src/index.js` or build with your bundler

Basic 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 have data-priority. Images with data-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 the img element 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-webp when available.
  • Uses IntersectionObserver when present for efficiency. If not available, falls back to a scroll/resize based check using getBoundingClientRect.

Edge cases and notes

  • If no images match the selector the function logs a console.warn and exits gracefully.
  • Ensure your img elements start with a placeholder (for example a tiny inline image or src="data:image/gif;base64,...") or no src until 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.