@mapedy/lazy-loading

1.0.2 • Public • Published

@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/lazy-loading@1.0.0/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/lazy-loading@1.0.0/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

Package Sidebar

Install

npm i @mapedy/lazy-loading

Weekly Downloads

30

Version

1.0.2

License

MIT

Unpacked Size

10.9 kB

Total Files

3

Last publish

Collaborators

  • mapedy