Finally, a simple, lightweight (0.6kb), pure JavaScript image lazy loader that even works in IE* using the IntersectionObserver API

Overview

Simply Lazy

A simple & lightweight (0.6kb), pure JavaScript image lazy loader that even works in IE* utilizing the built in IntersectionObserver API in your favorite browser.

See the Browser Support section for more information regarding browser support.

Live DEMO

View the live demo here.

This demo is hosted in this same repository's /docs directory for you to see exactly how it is used.

Install through npm:

npm i simply-lazy

Self Hosting:

Lazy loading doesn't have to be complicated. It's as simple as:

  1. Adding the script:
<script type="text/javascript" src="./simplyLazy.min.js"></script>
  1. Setting your images up with a data-src attribute:
<img data-src="/path/to/img.jpg" />
  1. Initialize the lazy loader:
SimplyLazy(optionalOptions).lazy(selector)

Any selector that works for querySelectorAll will work for the selector.

For example:

// Using a CSS class:
SimplyLazy(optionalOptions).lazy('.lazyload')

// Using a data attribute:
SimplyLazy(optionalOptions).lazy('[data-lazy]')

Additionally there is an options object you can use to get callbacks or set default information.

Callbacks

Function Description
onImageLoad(imgEl) Called after an image loads and it will receive the imageElement as a parameter
onImageError(imgEl) Called if there is an error loading the image (i.e. 404), it will receive the imageElement as a parameter

Parameters

Parameter Type Values Default
defaultImage String path to default image or base64 string null

defaultImage is only called onImageError and if it has a value. This can be useful if there is an error loading an image like a 404 but you still want to show a default placeholder image. The value you use for defaultImage will be set to the src of the <img> element. The example below is a base64 string of an empty image placeholder.

Here is an example usage with all of the options:

SimplyLazy({
  onImageLoad: (imgEl) => {
    // Called after an image loads
  },
  onImageError: (imgEl) => {
    // Called if there is an error loading the image
  },
  defaultImage: 'data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==' // An empty image, can be replaced with a known image URL
}).lazy(selector);

The selector can be any selector you would typically use when using el.querySelectorAll, like a class name.

In your HTML you only need to set a data-src equal to your image src. Note: Do not set the src or else you will override this plugin!

<img data-src"/path/to/img.jpg" />

Browser Support

This is supported in all major browsers and will even work in IE with one simple tweak.

For IE to work you must add this Polyfill script to your app which will only be loaded in browsers that do not support IntersectionObserver (like IE). All other browsers will not load anything when using this script from Polyfill.io:

<script src="https://polyfill.io/v3/polyfill.min.js?features=IntersectionObserver"></script>

Contributions

Contributions are always welcome. Please fork this repo and submit a PR.

Find a bug or want a new feature? Create an issue or a feature request here.

Contributors

Max Poshusta

You might also like...

Lazy minting of ERC721 NFTs using EIP712 standard for typed, structured data. ✨

ERC721 - Signature minting Lazy minting of ERC721 NFTs using EIP712 standard for typed, structured data. ✨ How it works Lazy minting or Signature mint

Oct 20, 2022

Simple JavaScript Library to add parallax image to background-image

Simple JavaScript Library to add parallax image to background-image

Backpax Simple JavaScript Library to add parallax image to background-image Install $ npm install backpax --save Demo Demo page is here Usage If you

Oct 12, 2022

JSPro is nothing but JavaScript Prototypes! The publisher is too lazy to write full name that's why it's just JSPro.

JSPro is nothing but JavaScript Prototypes! The publisher is too lazy to write full name that's why it's just JSPro. Whatever, it's a library of hundreds of awesome JavaScript Prototypes (you may know it as dot function) for lazy programmers. Just install the package with a little effort and leave the blames for the publisher.

Mar 10, 2022

Fancytree - JavaScript tree view / tree grid plugin with support for keyboard, inline editing, filtering, checkboxes, drag'n'drop, and lazy loading

Fancytree - JavaScript tree view / tree grid plugin with support for keyboard, inline editing, filtering, checkboxes, drag'n'drop, and lazy loading

Fancytree Fancytree (sequel of DynaTree 1.x) is a JavaScript tree view / tree grid plugin with support for keyboard, inline editing, filtering, checkb

Jan 9, 2023

Download Notion pages as markdown and image files, preserving hierarchy and enabling workflow properties. Works with Docusaurus.

Download Notion pages as markdown and image files, preserving hierarchy and enabling workflow properties. Works with Docusaurus.

notion-pull notion-pull lets you use Notion as your editor for markdown-based static site generators like Docusaurus. Using Notion instead of raw mark

Jan 7, 2023

A totally functional user api. It's a service where you list users, create users, update or even delete them.

USER-API 🎯 ABOUT A user api system made with TypeScript using express and prisma. It's a service where you list user, create users, update them or ev

Oct 27, 2022

A lazy plugin for printing local network when starting NodeJS server. I made this so you don't have to.

A lazy plugin for printing local network when starting NodeJS server. I made this so you don't have to.

lazy-net A simple, lightweight plugin for printing local network when starting Node.js or Express.js server. I made this so you don't have to. There a

Feb 10, 2022

Lazy evaluation list with high tree-shaking affinity and easy customization.

Lazy evaluation list with high tree-shaking affinity and easy customization.

Lazy evaluation list with high tree-shaking affinity and easy customization. Features 🎁 Lazy Evaluation: The collections are only enumerated to the m

Dec 28, 2022

A small (~600B gzip), useful set of methods for lazy iteration of iterables.

@ricokahler/lazy · A small (~600B gzip*), useful set of methods for lazy iteration of iterables. Why this lazy lib? Do I even need a lazy lib? Install

Sep 10, 2022
Owner
Max
Max
Fast and lightweight dependency-free vanilla JavaScript polyfill for native lazy loading / the awesome loading='lazy'-attribute.

loading="lazy" attribute polyfill Fast and lightweight vanilla JavaScript polyfill for native lazy loading, meaning the behaviour to load elements rig

Maximilian Franzke 571 Dec 30, 2022
High performance and SEO friendly lazy loader for images (responsive and normal), iframes and more, that detects any visibility changes triggered through user interaction, CSS or JavaScript without configuration.

lazysizes lazysizes is a fast (jank-free), SEO-friendly and self-initializing lazyloader for images (including responsive images picture/srcset), ifra

Alexander Farkas 16.6k Jan 1, 2023
Very useful website application to memorize all the tasks of the day, mark the complete ones, organize them by importance and finally delete them.

To Do List Very useful website application to memorize all the tasks of the day, mark the complete ones, organize them by importance and finally delet

Gonzalo Medina 3 Jul 29, 2022
Finally, a "back to top" button that behaves like a real elevator.

elevator.js Finally, a "back to top" button that behaves like a real elevator, by adding elevator music to quietly soothe the awkwardness that can ens

Tim Holman 6.6k Dec 27, 2022
This is a simple Image popup Jquery plugin. With a very simple configuration, you can show a popup on your webpage. By the way, this plugin works after page load.

Jquery-SingleImagePopup This is a simple Image popup Jquery plugin. With a very simple configuration, you can show a popup on your webpage. By the way

Rajan Karmaker 1 Aug 22, 2022
optimize image & upload file to cloud as image bed with tiny image automic.

Rush! 图片压缩 & 直传图床工具 这是一个兴趣使然的项目, 希望 Rush! 能让这个世界的网络资源浪费减少一点点 下载 Downloads 获取最新发行版 功能 Features 拖拽批量压缩图片, 支持格式 jpg/png/gif Drop to optimize, jpg/png/gif

{ Chao } 3 Nov 12, 2022
Hacktoberfest is all about meeting up all brains. In this repository we are planning to come with many ideas and works. You all can share your ides/works here.

Hacktoberfest Submit your Work Hacktoberfest is all about meeting up all brains. In this repository we are planning to come with many ideas and works.

Chinmay Patil 3 Oct 5, 2022
Snowfall effect written in pure JavaScript. No additional libraries, no dependencies. Works in every modern browser.

pureSnow.js Snow falling slowly on a winter night. Probably the most calming and peaceful snowfall effect written in pure JS/CSS. (No SCSS). Inspired

null 20 Dec 29, 2022
🖼 Simple file-upload utility that shows a preview of the uploaded image. Written in TypeScript. No dependencies. Works well with or without a framework.

file-upload-with-preview ?? Simple file-upload utility that shows a preview of the uploaded image. Written in TypeScript. No dependencies. Works well

John Datserakis 427 Dec 26, 2022
Simple lazy responsive starter kit for CraftCMS 4 Projects.

Lazy Craft CMS 4 Boilerplate Simple lazy responsive starter kit for CraftCMS 4 Projects. Requirements RTFM Craft CMS 4 Requirements Configs Duplicate

null 4 Sep 2, 2022