All Projects → alterebro → Accessible Image Lazy Load

alterebro / Accessible Image Lazy Load

Licence: mit
😴 gandul! accessible lazy loading images

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Accessible Image Lazy Load

Jquery.lazy
A lightweight, fast, feature-rich, powerful and highly configurable delayed content, image and background lazy loading plugin for jQuery & Zepto.
Stars: ✭ 965 (+243.42%)
Mutual labels:  images, lazy-loading, lazyload, lazy
Lozad.js
🔥 Highly performant, light ~1kb and configurable lazy loader in pure JS with no dependencies for responsive images, iframes and more
Stars: ✭ 6,932 (+2366.9%)
Mutual labels:  images, lazy-loading, lazyload
Yall.js
A fast, flexible, and small SEO-friendly lazy loader.
Stars: ✭ 1,163 (+313.88%)
Mutual labels:  images, lazy-loading, lazyload
Vanilla Lazyload
LazyLoad is a lightweight, flexible script that speeds up your website by deferring the loading of your below-the-fold images, backgrounds, videos, iframes and scripts to when they will enter the viewport. Written in plain "vanilla" JavaScript, it leverages IntersectionObserver, supports responsive images and enables native lazy loading.
Stars: ✭ 6,596 (+2247.33%)
Mutual labels:  images, lazy-loading, lazyload
jekyll-loading-lazy
🧙🏽‍♀️ Automatically adds loading="lazy" to <img> and <iframe> tags. Load images on your sites lazily without JavaScript.
Stars: ✭ 41 (-85.41%)
Mutual labels:  lazy, lazyload, lazy-loading
lazy-load-images.js
Progressive & lazy loading images.
Stars: ✭ 17 (-93.95%)
Mutual labels:  lazy, lazyload, lazy-loading
Responsively Lazy
Lazy load responsive images
Stars: ✭ 1,080 (+284.34%)
Mutual labels:  lazy-loading, lazyload, lazy
Bootstrap Vue
BootstrapVue provides one of the most comprehensive implementations of Bootstrap v4 for Vue.js. With extensive and automated WAI-ARIA accessibility markup.
Stars: ✭ 13,603 (+4740.93%)
Mutual labels:  accessibility, accessible
Lity
Lightweight, accessible and responsive lightbox.
Stars: ✭ 1,051 (+274.02%)
Mutual labels:  accessibility, accessible
Ngx Lazy Load Images
Image lazy load library for Angular 2+
Stars: ✭ 77 (-72.6%)
Mutual labels:  images, lazy-loading
guide-to-async-components
📖 Guide To JavaScript Async Components
Stars: ✭ 79 (-71.89%)
Mutual labels:  lazyload, lazy-loading
React Accessible Accordion
Accessible Accordion component for React
Stars: ✭ 610 (+117.08%)
Mutual labels:  accessibility, accessible
React Lazy Images
🖼️ 🛋️ Components and utilities for lazy image loading in React
Stars: ✭ 254 (-9.61%)
Mutual labels:  images, lazy-loading
rocket-lazy-load
Standalone LazyLoad plugin for WordPress (based on WP Rocket)
Stars: ✭ 25 (-91.1%)
Mutual labels:  lazyload, lazy-loading
loadeer
🦌 Tiny, performant, SEO-friendly lazy loading library
Stars: ✭ 30 (-89.32%)
Mutual labels:  lazyload, lazy-loading
lazysimon
Minimal effort 350 byte JavaScript library to lazy load all <img> on your website
Stars: ✭ 18 (-93.59%)
Mutual labels:  lazyload, lazy-loading
magento2-catalog-lazy-load
Improve the load time of your Magento 2 categories pages by loading your images on demand with our Lazy Load Extension
Stars: ✭ 56 (-80.07%)
Mutual labels:  lazyload, lazy-loading
Optikey
OptiKey - Full computer control and speech with your eyes
Stars: ✭ 3,906 (+1290.04%)
Mutual labels:  accessibility, accessible
Reakit
Toolkit for building accessible rich web apps with React
Stars: ✭ 5,265 (+1773.67%)
Mutual labels:  accessibility, accessible
relaze
Tiny image lazy loading library for React.
Stars: ✭ 20 (-92.88%)
Mutual labels:  lazy, lazy-loading
gandul

gandul! 😴 (accessible-image-lazy-load)

MIT license NPM Version File Size Twitter


Accessible lazy loading images

gandul 😴 is a lightweight (<2Kb) non-dependant javascript module that adds a different approach on lazy loading focusing on accessibility. Most existing options work by either making you drop the src attribute of the image or, making you create a base64 data / low resolution blurred alternative version of the image, or also including the img element into a <noscript> tag. This could be hacky and verbose and the main issue with it is that alters the semantics of the original element.

In order to avoid that, gandul 😴 works by taking a common anchor hyperlink <a> as the data source of the image to be loaded and transforms it into a <img> element. This way you don't lose the reference to the image you want to show and, in case there's no JavaScript, your image will still be accessible by users and crawlers.

Basic Example :

<!-- How your HTML will look like: -->
<a href="http://placekitten.com/320/180" class="gandul">Nice kitten</a>

<!-- What the gandul script will output : -->
<img src="http://placekitten.com/320/180" class="gandul" alt="Nice kitten" />
HTML Input gandul 😴 Output
<a href="http://placekitten.com/320/180" class="gandul">Nice kitten</a> <img src="http://placekitten.com/320/180" class="gandul" alt="Nice kitten" />
Nice kitten Nice kitten

How to use it

1. Download the script

You have multiple options to get gandul 😴 ( gandul.min.js ):

2. Get it working

Write an anchor hyperlink with the reference to your image and set the class gandul on it (<a class="gandul">). then, include and call the script :

<!-- How a gandul image reference looks like -->
<a href="http://placekitten.com/200/200" class="gandul">Nice kitten</a>

<!-- Include and call the script -->
<script src="https://unpkg.com/accessible-image-lazy-load"></script>
<script>gandul();</script>

Image Attributes

gandul 😴 will take all the existing attributes on the <a> element and they will be passed to the newly created <img> with a few peculiarities, as <a> elements don't have srcset, sizes, width... attributes, those will be passed as data attributes. Right below you can see the equivalences table and some examples:

In case you want to apply gandul to a different HTML element than a hyperlink (<a>) the image source will be taken from the data-href attribute

<a> attributes <img> attributes equivalence
Anchor hyperlink inner text. alt
href src
data-href src
data-srcset srcset
data-sizes sizes
data-width width

Fully responsive image example:

<!-- Write your hyperlink HTML as it follows : -->
<a href="http://placekitten.com/800/400"
   data-srcset="http://placekitten.com/320/160 320w,
             http://placekitten.com/480/240 480w,
             http://placekitten.com/800/400 800w"
   data-sizes="(max-width: 320px) 280px, (max-width: 480px) 440px, 800px"
   class="gandul">Nice kitten</a>

<!-- To get a responsive image output like this one: -->
<img src="http://placekitten.com/800/400"
	 srcset="http://placekitten.com/320/160 320w,
             http://placekitten.com/480/240 480w,
             http://placekitten.com/800/400 800w"
     sizes="(max-width: 320px) 280px, (max-width: 480px) 440px, 800px"
     class="gandul"
     alt="Nice kitten" />

Setting just the width attribute of an image :

<!-- What you'll write: -->
<a href="http://placekitten.com/800/400" data-width="800" class="gandul">Nice kitten</a>

<!-- What you'll get : -->
<img src="http://placekitten.com/800/400" width="800" alt="Nice kitten" />

Any other attributes :

Any other attributes will remain as they are, which means that if you already set or you need on the <img> an id or different classes, those will be passed through the gandul 😴 script :

<!-- input -->
<a href="http://placekitten.com/800/400" id="cat" class="nice kitten gandul">Nice kitten</a>

<!-- gandul output -->
<img src="http://placekitten.com/800/400" id="cat" class="nice kitten gandul" alt="Nice kitten" />

Options

Some parameters can be send to the gandul 😴 function:

gandul(target, opts, action);
  • target (@string) : a selector targeting all the elements where you want the script to get executed. It defaults to all anchor hyperlinks with the classname gandul : "a.gandul"
  • opts (@object) : an options object containing the fields used by the IntersectionObserver constructor
    • root : element used as viewport of the target. Default value is the brwoser viewport (null)
    • rootMargin : margin of root element to grow or shrink the intersection. Default value takes an extra 50 pixels above and below the viewport ("50px 0px 50px 0px").
    • threshold : percentage of target visibility to trigger the action. Default is 0.
  • action (@function) : A callback function to be executed when the image finishes loading after the target has intersected the given viewport, it comes with image element itself as first parameter (function(img){ /* console.log(img) */ }). The default action when image loads is to attach to the img element the class named 'gandul-active'.

The following example makes use of some of these options, it will target all hyperlinks with the class gandul-hyperlink, will be activated when 50% of the target element is visible and the function used as callback will change the created image border style as defined below:

gandul('a.gandul-hyperlink', { threshold: .5 }, function(img) {
    img.style.border = 'solid red 10px';
});

Development

# Clone the repo
$ git clone https://github.com/alterebro/accessible-image-lazy-load.git
$ cd accessible-image-lazy-load/

# Install dependencies
$ npm install

# Build (any of the below will do it)
$ npm run build
$ gulp

Gandul. Accessible Image Lazy loading
Jorge Moreno — @alterebro

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].