Skip to content

Commit

Permalink
Merge branch 'release/17.6.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
verlok committed Feb 20, 2022
2 parents 1497484 + b3d506f commit 7c13313
Show file tree
Hide file tree
Showing 20 changed files with 228 additions and 33 deletions.
7 changes: 7 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
},
"editor.tabSize": 4,
"editor.defaultFormatter": "esbenp.prettier-vscode"
}
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@

## Version 17


#### 17.6.0

- Added ability to lazily load the `<object>` tag, as requested in #177. Useful to lazily load animated SVGs.

#### 17.5.1

- Updated Typescript typings

#### 17.5.0

- Added the ability to restore DOM to its original state throught the `restoreAll()` method.
- Added the ability to restore DOM to its original state through the `restoreAll()` method.
- Destroy demo became [restore and destroy demo](demos/restore_destroy.html)

#### 17.4.0
Expand Down
28 changes: 21 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
LazyLoad is a lightweight (2.4 kB) and flexible script that **speeds up your web application** by deferring the loading of your below-the-fold images, videos and iframes to **when they will enter the viewport**. It's written in plain "vanilla" JavaScript, it leverages the [IntersectionObserver](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API) API, it supports [responsive images](https://alistapart.com/article/responsive-images-in-practice), it optimizes your website for slower connections, and can enable native lazy loading. See [all features](#-all-features-compared) for more.
LazyLoad is a lightweight (2.4 kB) and flexible script that **speeds up your web application** by deferring the loading of your below-the-fold images, animated SVGs, videos and iframes to **when they will enter the viewport**. It's written in plain "vanilla" JavaScript, it leverages the [IntersectionObserver](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API) API, it supports [responsive images](https://alistapart.com/article/responsive-images-in-practice), it optimizes your website for slower connections, and can enable native lazy loading. See [all features](#-all-features-compared) for more.

[![vanilla-lazyload (latest)](https://img.shields.io/npm/v/vanilla-lazyload/latest.svg)](https://www.npmjs.com/package/vanilla-lazyload)
[![vanilla-lazyload (downloads)](https://img.shields.io/npm/dy/vanilla-lazyload.svg)](https://www.npmjs.com/package/vanilla-lazyload)
Expand Down Expand Up @@ -127,6 +127,12 @@ Multiple backgrounds, HiDPI screen support:

ℹ Please note that you must use `url()` to wrap the URLs in your `data-bg-multi-hidpi` attributes.

#### Lazy animated SVG

```html
<object class="lazy" type="image/svg+xml" data-src="lazy.svg"></object>
```

#### Lazy video

```html
Expand All @@ -142,7 +148,7 @@ Please note that the video poster can be lazily loaded too.
#### Lazy iframe

```html
<iframe class="lazy" data-src="lazyFrame.html"> </iframe>
<iframe class="lazy" data-src="lazyFrame.html"></iframe>
```

---
Expand All @@ -153,7 +159,7 @@ Please note that the video poster can be lazily loaded too.

## 👩‍💻 Getting started - Script

The latest, recommended version of LazyLoad is **17.5.1**.
The latest, recommended version of LazyLoad is **17.6.0**.

Quickly understand how to upgrade from a previous version reading the [practical upgrade guide](UPGRADE.md).

Expand All @@ -162,7 +168,7 @@ Quickly understand how to upgrade from a previous version reading the [practical
The easiest way to use LazyLoad is to include the script from a CDN:

```html
<script src="https://cdn.jsdelivr.net/npm/vanilla-lazyload@17.5.1/dist/lazyload.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vanilla-lazyload@17.6.0/dist/lazyload.min.js"></script>
```

Then, in your javascript code:
Expand Down Expand Up @@ -203,7 +209,7 @@ Then include the script.
```html
<script
async
src="https://cdn.jsdelivr.net/npm/vanilla-lazyload@17.5.1/dist/lazyload.min.js"
src="https://cdn.jsdelivr.net/npm/vanilla-lazyload@17.6.0/dist/lazyload.min.js"
></script>
```

Expand Down Expand Up @@ -237,7 +243,7 @@ Then include the script.
```html
<script
async
src="https://cdn.jsdelivr.net/npm/vanilla-lazyload@17.5.1/dist/lazyload.min.js"
src="https://cdn.jsdelivr.net/npm/vanilla-lazyload@17.6.0/dist/lazyload.min.js"
></script>
```

Expand Down Expand Up @@ -371,6 +377,7 @@ HTML
<img class="lazy" alt="A lazy image" data-src="lazy.jpg" />
<iframe class="lazy" data-src="lazyFrame.html"></iframe>
<video class="lazy" controls data-src="lazy.mp4" data-poster="lazy.jpg">...</video>
<object class="lazy" type="image/svg+xml" data-src="lazy.svg"></object>
<div class="lazy" data-bg="lazy.jpg"></div>
```

Expand Down Expand Up @@ -585,6 +592,8 @@ The [demos](https://github.com/verlok/vanilla-lazyload/tree/master/demos) folder
| Content | Lazy video with multiple `<source>` tags, different preload options, NO autoplay | [Code](demos/video.html) | [Live](https://www.andreaverlicchi.eu/vanilla-lazyload/demos/video.html) |
| Content | Lazy video with multiple `<source>` tags, different preload options, WITH autoplay | [Code](demos/video_autoplay.html) | [Live](https://www.andreaverlicchi.eu/vanilla-lazyload/demos/video_autoplay.html) |
| Content | Lazy loading background images | [Code](demos/background_images.html) | [Live](https://www.andreaverlicchi.eu/vanilla-lazyload/demos/background_images.html) |
| Content | Lazy loading iframes | [Code](demos/iframes.html) | [Live](https://www.andreaverlicchi.eu/vanilla-lazyload/demos/iframes.html) |
| Content | Lazy loading animated SVGs and PDF files | [Code](demos/objects.html) | [Live](https://www.andreaverlicchi.eu/vanilla-lazyload/demos/objects.html) |
| Content | Lazy loading multiple background images | [Code](demos/background_images_multi.html) | [Live](https://www.andreaverlicchi.eu/vanilla-lazyload/demos/background_images_multi.html) |
| Content | Lazy WebP images with the `<picture>` tag and the `type` attribute for WebP | [Code](demos/picture_type_webp.html) | [Live](https://www.andreaverlicchi.eu/vanilla-lazyload/demos/picture_type_webp.html) |
| Loading | Asynchronous loading LazyLoad with requireJS | [Code](demos/amd.html) | [Live](https://www.andreaverlicchi.eu/vanilla-lazyload/demos/amd.html) |
Expand Down Expand Up @@ -688,7 +697,7 @@ Here's the list of the options.
| `class_error` | The class applied to the elements when the element causes an error. | `"error"` | `"lazy-error"` |
| `class_entered` | The class applied to the elements after they entered the viewport. | `"entered"` | `"lazy-entered"` |
| `class_exited` | The class applied to the elements after they exited the viewport. This class is removed if an element enters the viewport again. The `unobserve_entered` option can affect the appliance of this class, e.g. when loading images that complete loading before exiting. | `"exited"` | `"lazy-exited"` |
| `cancel_on_exit` | A boolean that defines whether or not to cancel the download of the images that exit the viewport while they are still loading, eventually restoring the original attributes. It applies only to images so to the `img` (and `picture`) tags, so it doesn't apply to background images, `iframe`s nor `video`s. | `true` | `false` |
| `cancel_on_exit` | A boolean that defines whether or not to cancel the download of the images that exit the viewport while they are still loading, eventually restoring the original attributes. It applies only to images so to the `img` (and `picture`) tags, so it doesn't apply to background images, `iframe`s, `object`s nor `video`s. | `true` | `false` |
| `unobserve_entered` | A boolean that defines whether or not to automatically unobserve elements once they entered the viewport | `false` | `true` |
| `unobserve_completed` | A boolean that defines whether or not to automatically unobserve elements once they've loaded or throwed an error | `true` | `false` |
| `callback_enter` | A callback function which is called whenever an element enters the viewport. Arguments: DOM element, intersection observer entry, lazyload instance. | `null` | `(el)=>{console.log("Entered", el)}` |
Expand Down Expand Up @@ -756,6 +765,7 @@ A list of all vanilla-lazyload features, compared with other popular lazy loadin
| Can lazyload responsive images |||
| ...and automatically calculate the value of the `sizes` attribute | ||
| Can lazyload iframes |||
| Can lazyload animated SVGs || |
| Can lazyload videos || |
| Can lazyload background images || |
| Can lazily execute code, when given elements enter the viewport || |
Expand Down Expand Up @@ -800,6 +810,10 @@ vanilla-lazyload doesn't have this feature because of performance optimization r

Both scripts can lazyload the `iframe` tag.

#### Can lazyload animated SVGs

Only vanilla-lazyload can load animated SVGs via the `object` tag.

#### Can lazyload videos

Only vanilla-lazyload can lazyload the `video` tag, even with multiple `source`s.
Expand Down
99 changes: 99 additions & 0 deletions demos/object.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8" />
<title>
Lazy object - Lazyload demos
</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<style>
object {
width: 100%;
height: 400px;
display: block;
margin: 1rem 0;
}
</style>
</head>

<body>
<h1>Lazy objects demo</h1>
<div id="results1" class="results">
<p>Scroll down to see lazily loaded objects</p>
<p>Scroll down to see lazily loaded objects</p>
<p>Scroll down to see lazily loaded objects</p>
<p>Scroll down to see lazily loaded objects</p>
<p>Scroll down to see lazily loaded objects</p>
<p>Scroll down to see lazily loaded objects</p>
<p>Scroll down to see lazily loaded objects</p>
<p>Scroll down to see lazily loaded objects</p>
<p>Scroll down to see lazily loaded objects</p>
<p>Scroll down to see lazily loaded objects</p>
<p>Scroll down to see lazily loaded objects</p>
<p>Scroll down to see lazily loaded objects</p>
<p>Scroll down to see lazily loaded objects</p>
<p>Scroll down to see lazily loaded objects</p>
<p>Scroll down to see lazily loaded objects</p>
<p>Scroll down to see lazily loaded objects</p>
<p>Scroll down to see lazily loaded objects</p>
<p>Scroll down to see lazily loaded objects</p>
<p>Scroll down to see lazily loaded objects</p>
<p>Scroll down to see lazily loaded objects</p>
<p>Scroll down to see lazily loaded objects</p>
<p>Scroll down to see lazily loaded objects</p>
<p>Scroll down to see lazily loaded objects</p>
<p>Scroll down to see lazily loaded objects</p>
<p>Scroll down to see lazily loaded objects</p>
<p>Scroll down to see lazily loaded objects</p>
<p>Scroll down to see lazily loaded objects</p>
<p>Scroll down to see lazily loaded objects</p>
<p>Scroll down to see lazily loaded objects</p>
<p>Scroll down to see lazily loaded objects</p>
<object type="image/svg+xml" class="lazy" data-src="objects/svg01.svg" title="Vecta.io demo image representing an application"></object>
<object type="application/pdf" class="lazy" data-src="objects/pdf01.pdf" title="A simple demonstration PDF file"></object>
</div>
<script src="../dist/lazyload.min.js"></script>
<script>
(function () {
function logElementEvent(eventName, element) {
console.log(Date.now(), eventName, element.getAttribute("data-src"));
}

var callback_enter = function (element) {
logElementEvent("🔑 ENTERED", element);
};
var callback_exit = function (element) {
logElementEvent("🚪 EXITED", element);
};
var callback_loading = function (element) {
logElementEvent("⌚ LOADING", element);
};
var callback_loaded = function (element) {
logElementEvent("👍 LOADED", element);
};
var callback_error = function (element) {
logElementEvent("💀 ERROR", element);
element.src =
"https://via.placeholder.com/440x560/?text=Error+Placeholder";
};
var callback_finish = function () {
logElementEvent("✔️ FINISHED", document.documentElement);
};
var callback_cancel = function (element) {
logElementEvent("🔥 CANCEL", element);
};

ll = new LazyLoad({
// Assign the callbacks defined above
callback_enter: callback_enter,
callback_exit: callback_exit,
callback_cancel: callback_cancel,
callback_loading: callback_loading,
callback_loaded: callback_loaded,
callback_error: callback_error,
callback_finish: callback_finish
});
})();
</script>
</body>
</html>
Binary file added demos/objects/pdf01.pdf
Binary file not shown.
1 change: 1 addition & 0 deletions demos/objects/svg01.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 16 additions & 3 deletions dist/lazyload.amd.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ define(function () { 'use strict';
var SIZES = "sizes";
var POSTER = "poster";
var ORIGINALS = "llOriginalAttrs";
var DATA = "data";

var statusLoading = "loading";
var statusLoaded = "loaded";
Expand Down Expand Up @@ -264,6 +265,7 @@ define(function () { 'use strict';
var attrsSrc = [SRC];
var attrsSrcPoster = [SRC, POSTER];
var attrsSrcSrcsetSizes = [SRC, SRCSET, SIZES];
var attrsData = [DATA];
var hasOriginalAttrs = function hasOriginalAttrs(element) {
return !!element[ORIGINALS];
};
Expand All @@ -275,6 +277,8 @@ define(function () { 'use strict';
}; // ## SAVE ##

var setOriginalsObject = function setOriginalsObject(element, attributes) {
debugger;

if (hasOriginalAttrs(element)) {
return;
}
Expand Down Expand Up @@ -378,6 +382,10 @@ define(function () { 'use strict';
setAttributeIfValue(videoEl, SRC, getData(videoEl, settings.data_src));
videoEl.load();
};
var setSourcesObject = function setSourcesObject(object, settings) {
setOriginalsObject(object, attrsData);
setAttributeIfValue(object, DATA, getData(object, settings.data_src));
};
var setBackground = function setBackground(element, settings, instance) {
var bg1xValue = getData(element, settings.data_bg);
var bgHiDpiValue = getData(element, settings.data_bg_hidpi);
Expand Down Expand Up @@ -405,7 +413,8 @@ define(function () { 'use strict';
var setSourcesFunctions = {
IMG: setSourcesImg,
IFRAME: setSourcesIframe,
VIDEO: setSourcesVideo
VIDEO: setSourcesVideo,
OBJECT: setSourcesObject
};
var setSourcesNative = function setSourcesNative(element, settings) {
var setSourcesFunction = setSourcesFunctions[element.tagName];
Expand All @@ -427,7 +436,7 @@ define(function () { 'use strict';
manageLoading(element, settings, instance);
};

var elementsWithLoadEvent = ["IMG", "IFRAME", "VIDEO"];
var elementsWithLoadEvent = ["IMG", "IFRAME", "VIDEO", "OBJECT"];
var hasLoadEvent = function hasLoadEvent(element) {
return elementsWithLoadEvent.indexOf(element.tagName) > -1;
};
Expand Down Expand Up @@ -569,10 +578,14 @@ define(function () { 'use strict';
var restoreIframe = function restoreIframe(iframeEl) {
restoreOriginalAttrs(iframeEl, attrsSrc);
};
var restoreObject = function restoreObject(objectEl) {
restoreOriginalAttrs(objectEl, attrsData);
};
var restoreFunctions = {
IMG: restoreImg,
IFRAME: restoreIframe,
VIDEO: restoreVideo
VIDEO: restoreVideo,
OBJECT: restoreObject
};

var restoreAttributes = function restoreAttributes(element) {
Expand Down
Loading

0 comments on commit 7c13313

Please sign in to comment.