Gathering detailed insights and metrics for lozad
Gathering detailed insights and metrics for lozad
Gathering detailed insights and metrics for lozad
Gathering detailed insights and metrics for lozad
npm install lozad
Typescript
Module System
Node Version
NPM Version
98.5
Supply Chain
100
Quality
76.1
Maintenance
100
Vulnerability
100
License
JavaScript (100%)
Total Downloads
8,882,427
Last Day
1,170
Last Week
31,779
Last Month
127,202
Last Year
1,507,000
7,465 Stars
245 Commits
448 Forks
115 Watching
13 Branches
37 Contributors
Latest Version
1.16.0
Package Id
lozad@1.16.0
Size
8.84 kB
NPM Version
6.13.4
Node Version
8.17.0
Publised On
06 Sept 2020
Cumulative downloads
Total Downloads
Last day
-33.3%
1,170
Compared to previous day
Last week
-2.6%
31,779
Compared to previous week
Last month
-0.4%
127,202
Compared to previous month
Last year
-7.9%
1,507,000
Compared to previous year
Highly performant, light and configurable lazy loader in pure JS with no dependencies for images, iframes and more, using IntersectionObserver API
Lozad.js:
It is written with an aim to lazy load images, iframes, ads, videos or any other element using the recently added Intersection Observer API with tremendous performance benefits.
and many more...
Existing lazy loading libraries hook up to the scroll event or use a periodic timer and call getBoundingClientRect()
on elements that need to be lazy loaded. This approach, however, is painfully slow as each call to getBoundingClientRect()
forces the browser to re-layout the entire page and will introduce considerable jank to your website.
Making this more efficient and performant is what IntersectionObserver is designed for, and it’s landed in Chrome 51. IntersectionObservers let you know when an observed element enters or exits the browser’s viewport.
1# You can install lozad with npm 2$ npm install --save lozad 3 4# Alternatively you can use Yarn 5$ yarn add lozad 6 7# Another option is to use Bower 8$ bower install lozad
Then with a module bundler like rollup or webpack, use as you would anything else:
1// using ES6 modules 2import lozad from 'lozad' 3 4// using CommonJS modules 5var lozad = require('lozad')
Or load via CDN and include in the head
tag of your page.
1<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/lozad/dist/lozad.min.js"></script>
When loading from CDN, you can find the library on window.lozad
.
In HTML, add an identifier to the element (default selector identified is lozad
class):
1<img class="lozad" data-src="image.png" />
All you need to do now is just instantiate Lozad as follows:
1const observer = lozad(); // lazy loads elements with default selector as '.lozad' 2observer.observe();
or with a DOM Element
reference:
1const el = document.querySelector('img'); 2const observer = lozad(el); // passing a `NodeList` (e.g. `document.querySelectorAll()`) is also valid 3observer.observe();
or with custom options:
1const observer = lozad('.lozad', { 2 rootMargin: '10px 0px', // syntax similar to that of CSS Margin 3 threshold: 0.1 // ratio of element convergence 4}); 5observer.observe();
Reference:
or if you want to give custom function definition to load element:
1lozad('.lozad', { 2 load: function(el) { 3 console.log('loading element'); 4 5 // Custom implementation to load an element 6 // e.g. el.src = el.getAttribute('data-src'); 7 } 8});
If you would like to extend the loaded
state of elements, you can add the loaded option:
Note: The
"data-loaded"="true"
attribute is used by lozad to determine if an element has been previously loaded.
1lozad('.lozad', { 2 loaded: function(el) { 3 // Custom implementation on a loaded element 4 el.classList.add('loaded'); 5 } 6});
If you want to lazy load dynamically added elements:
1const observer = lozad(); 2observer.observe(); 3 4// ... code to dynamically add elements 5observer.observe(); // observes newly added elements as well
for use with responsive images
1<!-- responsive image example --> 2<img class="lozad" data-src="image.png" data-srcset="image.png 1000w, image-2x.png 2000w" />
for use with background images
1<!-- background image example --> 2<div class="lozad" data-background-image="image.png"> 3</div>
for use with multiple background images
1<!-- multiple background image example --> 2<div class="lozad" data-background-image="path/to/first/image,path/to/second/image,path/to/third/image"> 3</div>
for use with responsive background images (image-set)
1<!-- responsive background image-set example --> 2<div class="lozad" data-background-image-set="url('photo.jpg') 1x, url('photo@2x.jpg') 2x"> 3</div>
To change the delimiter that splits background images:
1<!-- custom delimiter for background images example --> 2<div 3 class="lozad" 4 data-background-image="/first/custom,image,path/image.png-/second/custom,image,path/image.png" 5 data-background-delimiter="-" 6> 7</div>
If you want to load the images before they appear:
1const observer = lozad();
2observer.observe();
3
4const coolImage = document.querySelector('.image-to-load-first')
5// ... trigger the load of a image before it appears on the viewport
6observer.triggerLoad(coolImage);
Sometimes image loading takes a long time. For this case, you can add a placeholder background:
1<img class="lozad" data-placeholder-background="red" data-src="image.png" />
Lozad sets a placeholder background color of img element and users will see the fallback till the image loads.
Create a broken picture element structure.
IE browser don't support picture tag! You need to set
data-iesrc
attribute (only for your picture tags) with source for IE browser
data-alt
attribute can be added to picture tag for use inalt
attribute of lazy-loaded images
1<!-- For an element to be caught, add a block type that is different from the inline and some min-height for correct caught into view --> 2<picture class="lozad" style="display: block; min-height: 1rem" data-iesrc="images/thumbs/04.jpg" data-alt=""> 3 <source srcset="images/thumbs/04.jpg" media="(min-width: 1280px)"> 4 <source srcset="images/thumbs/05.jpg" media="(min-width: 980px)"> 5 <source srcset="images/thumbs/06.jpg" media="(min-width: 320px)"> 6 <!-- NO img element --> 7 <!-- instead of img element, there will be the last source with the minimum dimensions --> 8 <!-- for disabled JS you can set <noscript><img src="images/thumbs/04.jpg" alt=""></noscript> --> 9</picture>
When lozad loads this picture element, it will fix it.
If you want to use image placeholder (like low quality image placeholder), you can set a temporary img
tag inside your picture
tag. It will be removed when lozad loads the picture element.
1<picture class="lozad" style="display: block; min-height: 1rem" data-iesrc="images/thumbs/04.jpg" data-alt=""> 2 <source srcset="images/thumbs/04.jpg" media="(min-width: 1280px)"> 3 <source srcset="images/thumbs/05.jpg" media="(min-width: 980px)"> 4 <source srcset="images/thumbs/06.jpg" media="(min-width: 320px)"> 5 <!-- you can define a low quality image placeholder that will be removed when the picture is loaded --> 6 <img src="data:image/jpeg;base64,/some_lqip_in_base_64==" /> 7</picture>
1<video class="lozad" data-poster="images/backgrounds/video-poster.jpeg"> 2 <source data-src="video/mov_bbb.mp4" type="video/mp4"> 3 <source data-src="video/mov_bbb.ogg" type="video/ogg"> 4</video>
1<iframe data-src="embed.html" class="lozad"></iframe>
That's all, just add the lozad
class.
1<div data-toggle-class="active" class="lozad"> 2 <!-- content --> 3</div>
The active
class will be toggled on the element when it enters the browser’s viewport.
Available in latest browsers. If browser support is not available, then make use of polyfill.
For IE11 support, please make use of these polyfills.
Checkout the FAQ Wiki for some common gotchas to be aware of while using lozad.js
Interested in contributing features and fixes?
See the Changelog
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 7/14 approved changesets -- score normalized to 5
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
52 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-12-23
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More