/lazy-loading-img

Web component to lazy load images using Intersection Observer

Primary LanguageTypeScriptMIT LicenseMIT

Lazy Loading Image

Web component to lazy load images using Intersection Observer.

Getting Started

npm install
npm start

To watch for file changes during develop, run:

npm run dev

To build the component for production, run:

npm run build

To run the unit tests for the components, run:

npm test

Using this component

Polyfill

Not all browsers natively support IntersectionObserver. This can be supported with a polyfill such as Polyfill.io.

<script src="https://cdn.polyfill.io/v2/polyfill.js?features=IntersectionObserver,IntersectionObserverEntry"></script>

Script tag

  • Put a script tag similar to this <script src="https://unpkg.com/lazy-loading-img@latest/dist/lazyloadingimg.js"></script> in the head of your index.html
  • Then you can use the element anywhere in your template, JSX, html etc

Node Modules

  • Run npm install lazy-loading-img --save
  • Put a script tag similar to this <script src="node_modules/lazy-loading-img/dist/lazyloadingimg.js"></script> in the head of your index.html
  • Then you can use the element anywhere in your template, JSX, html etc

In a stencil-starter app

  • Run npm install lazy-loading-img --save
  • Add { name: 'lazy-loading-img' } to your collections
  • Then you can use the element anywhere in your template, JSX, html etc

HTML

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Lazy Loading Image Example</title>

  <!-- Intersection Observer Polyfill -->
  <script src="https://cdn.polyfill.io/v2/polyfill.js?features=IntersectionObserver,IntersectionObserverEntry"></script>
  <!-- <lazy-loading-image> Web Component -->
  <script src="node_modules/lazy-loading-img/dist/lazyloadingimg.js"></script>
</head>
<body>
  <lazy-loading-img src="http://placehold.it/400x300" alt="Lorem ipsum"></lazy-loading-img>
</body>
</html>

Built With Stencil

Stencil

Stencil is a compiler for building fast web apps using Web Components.

Stencil combines the best concepts of the most popular frontend frameworks into a compile-time rather than run-time tool. Stencil takes TypeScript, JSX, a tiny virtual DOM layer, efficient one-way data binding, an asynchronous rendering pipeline (similar to React Fiber), and lazy-loading out of the box, and generates 100% standards-based Web Components that run in any browser supporting the Custom Elements v1 spec.

Stencil components are just Web Components, so they work in any major framework or with no framework at all.