Giter Club home page Giter Club logo

progressive-img's Introduction

progressive-img

A simple javascript tool to enable progressive lazy-loading for images and background images. This tool utilizes the IntersectionObserver class where supported to lazy-load source files for <img> tags as well as background images for any matching tag.

Usage

Images: image elements matching the selector img[data-src] will be lazy-loaded and replace the original image source upon load. <img src="some-image.png"> => <img data-src="some-image.png">

Background Images: all elements matching the selector [data-bg-src] will be lazy-loaded and replace the original element inline style attribute with the background image url provided in the data-bg-src attribute. Example: <div style="background-image: url('some-image.png')">...</div> => <div data-bg-src="some-image.png"></div>.

Placeholders

For both images and background images, original sources will be overwritten but are still useful for adding low fidelity versions of the same image as a placeholder.

Example:

<img src="some-image-lowres.png" data-src="some-image.png">

Disabled Javascript Fallback

It is good to keep a <noscript> fallback in the unlikely case of a browser having Javascript disabled.

Example:

<noscript>
	<img src="some-image.png">
</noscript>
<img src="some-image-lowres.png" data-src="some-image.png">

Also, make sure to hide the progressive version:

<noscript><style>[data-src] { display: none; }</style></noscript>

Note: this snippet is only needed once

progressive-img's People

Contributors

jrm98 avatar

Watchers

 avatar

progressive-img's Issues

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.