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 🙏

© 2024 – Pkg Stats / Ryan Hefner

easy-lazyload-image

v0.0.3

Published

Structure for images where you can easily apply Lazy load to your images.

Downloads

8

Readme

Easy Lazy Load Image

LazyLoad is a lightweight JavaScript library for lazy loading images with various customization options. It improves performance by loading images only when they are about to enter the viewport.

Installation

You can install LazyLoad using npm:

npm install easy-lazy-load-image

or


<script src="https://cdn.jsdelivr.net/npm//[email protected]/index.js"></script>

Usage

To use LazyLoad, create a new instance of the class and pass the CSS selector of the container holding the images along with any desired options.

Basic Example

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>LazyLoad Example</title>
    <style>
        #image {
            display: flex;
            flex-direction: column;
            align-items: center;
        }

        ul {
            list-style-type: none;
        }

        li {
            width: 550px;
            height: 310px;
            margin: 10px;
            align-items: center;
            background-color: gray;
            border: 1px solid black;
        }

        img {
            width: 100%;
        }

    </style>
</head>
<body>
<div id="image-container">
    <img data-src="image1.jpg" alt="Image 1">
    <img data-src="image2.jpg" alt="Image 2">
</div>

<script src="https://cdn.jsdelivr.net/npm//[email protected]/index.js"></script>
<script>
    document.addEventListener('DOMContentLoaded', function () {
        new LazyLoad('#image-container', {
            threshold: 200,
            animation: 'fade',
            callback: (element) => console.log(`Loaded: ${element.src}`),
            delay: 300,
            placeholder: 'placeholder.jpg',
            errorImage: 'error.jpg',
            useIntersectionObserver: true,
            responsive: true
        });
    });
</script>
</body>
</html>

Options

  1. selector: The CSS selector to target elements for lazy loading. This is a required parameter.
  2. threshold: The distance in pixels from the viewport that triggers the loading of an image. The default value is 300.
  3. root: The element that is used as the viewport for checking visibility of the target elements. The default value is the browser viewport.
  4. callback: A callback function to execute after each image is loaded. Default is null.
  5. animation: The type of animation to use when loading images. Default is fade.
  6. animationDuration: The duration of the animation in milliseconds. Default is 500.
  7. delay: The delay in milliseconds before loading an image. Default is 500.
  8. placeholder: The URL of the placeholder image to use while the target image is loading. Default is null.
  9. errorPlaceholder: The URL of the placeholder image to use if the target image fails to load. Default is null.
  10. useIntersectionObserver: A boolean value indicating whether to use the Intersection Observer API. Default is false.
  11. responsive: Whether to support responsive images using srcset and sizes attributes. Default is false.

Methods

Constructor

Creates an instance of LazyLoad.

new LazyLoad(selector, options);
  • selector (string): The CSS selector to target elements for lazy loading.
  • options (object): An object containing customization options.

Example

new LazyLoad('#image-container', {
    threshold: 200,
    animation: 'fade',
    callback: (element) => console.log(`Loaded: ${element.src}`),
    delay: 300,
    placeholder: 'placeholder.jpg',
    errorImage: 'error.jpg',
    useIntersectionObserver: true,
    responsive: true
});