Coder Social home page Coder Social logo

smrubin / lazy-load-image Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 65 KB

A module to lazy load images using the Intersection Observer API.

License: MIT License

JavaScript 87.71% HTML 12.29%
intersection-observer intersectionobserver-api intersectionobserver lazy-loading lazyload lazyload-image lazy-load-img

lazy-load-image's Introduction

lazy-load-image

GitHub license

A module to lazy load images using the Intersection Observer API. Targets images with a specified DOMString selector. Specify distances from viewport at which to actually make the HTTP request to load the image(s).

Example

Install:

npm i -S @smrubin/lazy-load-image

Add data-lazyload attribute to img elements to be lazy loaded. Leave src attribute as empty string or use a loading indicator.

<body>
	<img class="targetSelector" src="" data-lazyload="http://placekitten.com/200/200" />
	<img class="targetSelector" src="" data-lazyload="./dog.png" />

    <img class="targetSelector" src="loading.gif" data-lazyload="./big_dog.png" />
</body>

Import and invoke the module.

import LazyLoad from 'lazy-load-image';

new LazyLoad('.targetSelector');

Options

new LazyLoad(selector, rooMargin)

selector - Required. A DOMString containing one or more selectors to match against.

rootMargin - Optional. Margin around the root (viewport). Can have values similar to the CSS margin property, e.g. "10px 20px 30px 40px". The values can be percentages. Defaults to 200px. With respect to lazy loading the image, how much margin between the element and the viewport before the image load is performed.

lazy-load-image's People

Contributors

smrubin avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.