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

@mapedy/lazy-loading

v1.0.2

Published

This package provides a lightweight and efficient lazy loading script for web developers. It uses the Intersection Observer API to defer loading images, iframes, and background images until they are within the viewport, improving page load times and reduc

Downloads

192

Readme

@mapedy/lazy-loading

This package provides a lightweight and efficient lazy loading script for web developers. It uses the Intersection Observer API to defer loading images, iframes, and background images until they are within the viewport, improving page load times and reducing unnecessary network requests. The script also includes smooth transition animations for a seamless user experience.

Features

  • Lazy Loading: Delays loading of images, iframes, and background images until they are visible on the user's screen.
  • Smooth Animations: Implements opacity and translate animations to create a smooth appearance effect when elements load.
  • Easy Integration: Simply add a lazy class to your HTML elements and let the script handle the rest.
  • Lightweight: Minimal footprint, leveraging native browser APIs for maximum efficiency.
  • Flexible: Can be re-invoked dynamically for content added after the initial page load.

Installation

You can install the package via npm:


npm  install  @mapedy/lazy-loading

Usage

HTML

Add the lazy class to your HTML elements:

<img class="lazy" src="image.jpg" alt="Lazy loaded image" />

<iframe class="lazy" src="video.mp4"></iframe>

<div class="lazy" style="background-image: url('background.jpg');"></div>

JavaScript

After installation, include the script in your project:

Using Node.js (CommonJS)

const lazyLoad = require("@mapedy/lazy-loading");
// Initialize lazy loading
document.addEventListener("DOMContentLoaded", function () {
  lazyLoad();
});

Using ES Modules

If you're using ES Modules in your project, you can import the script like this:

import lazyLoad from "@mapedy/lazy-loading";
// Initialize lazy loading
document.addEventListener("DOMContentLoaded", function () {
  lazyLoad();
});

Using a CDN

You can also use the package via a CDN:

<script src="https://unpkg.com/@mapedy/[email protected]/lazy.js"></script>
<script>
  // Initialize lazy loading
  document.addEventListener("DOMContentLoaded", function () {
    lazyLoad();
  });
</script>

Example

Here's a basic example of how to use the lazy loading script:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Lazy Loading Example</title>
  </head>
  <body>
    <img class="lazy" src="image.jpg" alt="Lazy loaded image" />
    <iframe class="lazy" src="video.mp4"></iframe>
    <div class="lazy" style="background-image: url('background.jpg');"></div>

    <script src="https://unpkg.com/@mapedy/[email protected]/lazy.js"></script>
    <script>
      // Initialize lazy loading
      document.addEventListener("DOMContentLoaded", function () {
        lazyLoad();
      });
    </script>
  </body>
</html>

Changelog

1.0.2

  • Replaced old name attribute with 'data-lazy-climbed' to ensure uniqueness and avoid conflicts with other plugins or libraries.
  • Added logic to check if an element supports lazy loading. If not, the script attempts to find the nearest child element that does. If no suitable element is found, the script marks the original element as 'unsupported' using the 'data-lazy-climbed' attribute.
  • Ensured compatibility with mobile devices and smart TVs by utilizing modern JavaScript features and providing fallback support for environments where IntersectionObserver is not available.
  • Improved code robustness by preventing animations from being applied to unsupported elements and refining lazy loading behavior.

Keywords

  • lazy load
  • intersection observer
  • image loading
  • performance
  • web development
  • optimization
  • javascript
  • lazyload
  • lazy-loading
  • images
  • iframes
  • background-images
  • scroll
  • viewport

License

This project is licensed under the MIT License - see the LICENSE file for details.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request or open an issue to improve this package.

Author

Alexey Kovalevsky