2.1.0 • Published 3 years ago

vtex-lazyloading v2.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

:rocket:Vtex Lazyloading

GitHub stars Npm package daily downloads

A vtex-lazyload based plugin for lazy loading Vtex components, other elements and images.

It also works for other platforms and projects.

:mag:Demo

https://codepen.io/akistapace/pen/ExRjgYw

:white_check_mark:Instalation

$ npm install vtex-lazyloading or download the file lazyloading.js

<script type="text/javascript" src="/arquivos/lazyloading.min.js"></script>

:wrench:Usage

Javascript

CDN

You can put this CDN in your HTML

<script src="https://cdn.jsdelivr.net/npm/vtex-lazyloading/lazyloading.min.js"></script>

And call it in your js file like this:point_down:

let lazyload = new VtexLazyload({options}) 

Or you can install this and export in your file

npm i vtex-lazyload
import vtexLazyload from "vtex-lazyloading";
const lazyload = new vtexLazyload({
    root: null,
    targets: '[data-lazy]',
    margin: '300px 20px',
    onRender: (e)=> {
        console.log('Rendering');
        if (e.classList.contains('target')) {
            e.style.opacity = 0.5;
        }
    },
});

HTML

For images and Iframes

<img data-lazy="https://via.placeholder.com/700x400" alt="placeholder"/></noscript>

<iframe data-lazy="https://www.youtube.com/embed/Y4goaZhNt4k" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>

For Vtex components

<div class="your-parentElement" data-lazy="true">
  <noscript>
    ...
  </noscript>
</div>
ParamsExampleDescription
targets#id, .class, atributesdefine all the lazy elements
rootnull, #id, .class, atributesthe parent container to trigger the lazy elements, if null is the entire viewport / window.
margin10pxis the distance in pixels for the element to be loaded before it becomes visible in the window
onRenderfunctionExecutes a callback when the element is loaded

Events

onRender: (e)=> {
    console.log('Rendering element');
}

Methods

 lazyload.update();
 lazyload.reinit();
 lazyload.destroy();
 lazyload.destroyInElement('#test');
ParamsDescription
update()Updates and picks up new items on the DOM
reinit()Restart the lazy loader if it has stopped with destroy or destroyInElement
destroy()Destroys the lazy loader instance in all elements
destroyInElement()Destroy the lazy load in an specific element

Custom styles

You can use the following classes to custom style:

.--lazy-waiting   { /* Uses when element is loading */ }
.--lazy-loaded    { /* Uses when element is visible */ }
.--lazy-triggered { /* Uses when element is tracked */ }
.--lazy-error     { /* Uses when element has an error */ }

Note: If the element is inside a parent element that is hidden, the lazy load will only occur when the parent element is visible, this is good for when we have images in menus.

:pencil2:Author

Fernando Oliveira Aquistapace - Linkedin

License

Open source licensed under the MIT license.

2.1.0

3 years ago

2.0.9

3 years ago

2.0.8

3 years ago

2.0.7

3 years ago

2.0.6

3 years ago

2.0.5

3 years ago

2.0.4

3 years ago

2.0.3

3 years ago

2.0.2

3 years ago

2.0.1

3 years ago

2.0.0

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago