smooth-loader 1.1.0 → 1.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +14 -0
- package/README.md +43 -19
- package/lib/js/handler.d.ts +8 -0
- package/lib/js/handler.d.ts.map +1 -0
- package/lib/js/handler.js +30 -0
- package/lib/js/handler.js.map +1 -0
- package/lib/js/imageLoader.d.ts +7 -0
- package/lib/js/imageLoader.d.ts.map +1 -0
- package/lib/js/imageLoader.js +31 -0
- package/lib/js/imageLoader.js.map +1 -0
- package/lib/js/index.d.ts +2 -2
- package/lib/js/index.d.ts.map +1 -1
- package/lib/js/index.js +16 -8
- package/lib/js/index.js.map +1 -1
- package/lib/js/loader.d.ts +8 -0
- package/lib/js/loader.d.ts.map +1 -0
- package/lib/js/loader.js +55 -0
- package/lib/js/loader.js.map +1 -0
- package/lib/js/types.d.ts +1 -0
- package/lib/js/types.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/handler.ts +32 -0
- package/src/imageLoader.ts +36 -0
- package/src/index.ts +21 -12
- package/src/types.ts +2 -2
- package/src/LazyLoader.ts +0 -80
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,20 @@
|
|
|
2
2
|
|
|
3
3
|
----
|
|
4
4
|
|
|
5
|
+
## v1.1.2 (2022-12-04)
|
|
6
|
+
|
|
7
|
+
- Removed line from docs;
|
|
8
|
+
|
|
9
|
+
----
|
|
10
|
+
|
|
11
|
+
## v1.1.1 (2022-12-04)
|
|
12
|
+
|
|
13
|
+
- Code refactoring;
|
|
14
|
+
- Added ability to provide array of images instead of only NodeListOf;
|
|
15
|
+
- Improved documentation (README.md);
|
|
16
|
+
|
|
17
|
+
----
|
|
18
|
+
|
|
5
19
|
## v1.1.0 (2022-12-01)
|
|
6
20
|
|
|
7
21
|
- Moved from beta version;
|
package/README.md
CHANGED
|
@@ -18,52 +18,74 @@ This package is a MUST-HAVE for every website containing multiple images on the
|
|
|
18
18
|
|
|
19
19
|
## 🎁 Features
|
|
20
20
|
|
|
21
|
-
-
|
|
22
|
-
-
|
|
23
|
-
-
|
|
24
|
-
-
|
|
25
|
-
-
|
|
21
|
+
- 😍 Well-documented;
|
|
22
|
+
- 😍 Written in TypeScript;
|
|
23
|
+
- 😍 Very small package;
|
|
24
|
+
- 😍 Loads images with IMG tag and backgrounds;
|
|
25
|
+
- 😍 Loads images with a smooth fade in animation;
|
|
26
|
+
- 😍 For image elements, you can show minified version of the image on initial page load;
|
|
27
|
+
- 😍 Configurable;
|
|
26
28
|
|
|
27
29
|
## ✍️ Usage
|
|
28
30
|
|
|
29
|
-
To make image
|
|
30
|
-
|
|
31
|
-
|
|
31
|
+
To make image element or background image lazy loaded:
|
|
32
|
+
- Add image URL to `data-src` attribute;
|
|
33
|
+
- Call `smoothLoader()` function;
|
|
32
34
|
|
|
33
|
-
|
|
34
|
-
- Call `smoothLoader()` function when you want to lazy load images.
|
|
35
|
+
#### Look at several options to choose images which you want to lazy load:
|
|
35
36
|
|
|
36
37
|
```js
|
|
37
|
-
|
|
38
|
+
import smoothLoader from 'smooth-loader'
|
|
39
|
+
|
|
40
|
+
// Default selector
|
|
41
|
+
// Selects all images with 'smooth-loader' class
|
|
38
42
|
smoothLoader()
|
|
39
43
|
|
|
40
|
-
//
|
|
44
|
+
// Custom selector
|
|
45
|
+
// Selects all images with 'lazy-image' class
|
|
41
46
|
smoothLoader('.lazy-image')
|
|
42
47
|
|
|
43
|
-
//
|
|
44
|
-
|
|
45
|
-
|
|
48
|
+
// Elements
|
|
49
|
+
// Selects all the images
|
|
50
|
+
const images = document.querySelectorAll<HTMLImageElement>('img')
|
|
51
|
+
smoothLoader(images)
|
|
52
|
+
|
|
53
|
+
// Certain elements
|
|
54
|
+
// Selects all the images
|
|
55
|
+
const myPhoto = document.querySelector<HTMLImageElement>('#me')
|
|
56
|
+
|
|
57
|
+
if (myPhoto) {
|
|
58
|
+
smoothLoader([myPhoto])
|
|
59
|
+
}
|
|
46
60
|
```
|
|
47
61
|
|
|
48
62
|
> Note, that lazy loading works only when your elements are already in the DOM. Meaning, if images are loaded after the `smoothLoader` was executed, lazy loading will not work.
|
|
49
63
|
|
|
50
64
|
```html
|
|
51
65
|
<!-- Regular image -->
|
|
52
|
-
<img
|
|
66
|
+
<img
|
|
67
|
+
src="./images/me-min.webp"
|
|
68
|
+
data-src="./images/me.webp"
|
|
69
|
+
class="smooth-loader"
|
|
70
|
+
alt="Photo of Serhii Cho"
|
|
71
|
+
/>
|
|
53
72
|
|
|
54
73
|
<!-- Background image -->
|
|
55
|
-
<div
|
|
74
|
+
<div
|
|
75
|
+
data-src="./images/we.png"
|
|
76
|
+
class="smooth-loader my-bg-image"
|
|
77
|
+
></div>
|
|
56
78
|
```
|
|
57
79
|
|
|
58
80
|
## 🌄 Image preview
|
|
59
81
|
|
|
60
|
-
You can add a small version of the image to `src` attribute on image element
|
|
82
|
+
You can add a small version of the image to `src` attribute on image element `<img src="here">`, so that users don't look at the empty place when image is loading. It can be useful if you have many images on the page or you have several large images which.
|
|
61
83
|
|
|
62
84
|
To use this feature, you'll need small versions of your images which will be loaded right away when the page loads. Let's say I have 2 same images with different sizes:
|
|
63
85
|
|
|
64
86
|

|
|
65
87
|
|
|
66
|
-
I want to load a small image on page load, and when person scrolls into the image view, the bigger version will be loaded and it will replace the small one. In my HTML I will simply add an `src` attribute with path to a small image.
|
|
88
|
+
I want to load a small image on the initial page load, and when person scrolls into the image view, the bigger version will be loaded and it will replace the small one. In my HTML I will simply add an `src` attribute with path to a small image.
|
|
67
89
|
|
|
68
90
|
```html
|
|
69
91
|
<img
|
|
@@ -78,6 +100,8 @@ I want to load a small image on page load, and when person scrolls into the imag
|
|
|
78
100
|
|
|
79
101
|
> It's important to add `width` and `height` attributes or have width and hight setup correctly with CSS. Because small image has to be filling all the space that large image is taking.
|
|
80
102
|
|
|
103
|
+
> This feature works only for image elements, background images do not support it yet!
|
|
104
|
+
|
|
81
105
|
Instead of the small version of your image you can use just a small placeholder image, and it will work fine.
|
|
82
106
|
|
|
83
107
|
## ⚙️ Configurations
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { Config, LazyImage } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Creates image object, gets attributes from placeholder,
|
|
4
|
+
* sets them on image object, adds classes to image and
|
|
5
|
+
* when image is loaded, appends it to a placeholder
|
|
6
|
+
*/
|
|
7
|
+
export default function (images: LazyImage[], config: Config): void;
|
|
8
|
+
//# sourceMappingURL=handler.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"handler.d.ts","sourceRoot":"","sources":["../../src/handler.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,SAAS,CAAA;AAG3C;;;;GAIG;AACH,MAAM,CAAC,OAAO,WAAW,MAAM,EAAE,SAAS,EAAE,EAAE,MAAM,EAAE,MAAM,GAAG,IAAI,CAElE"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const imageLoader_1 = require("./imageLoader");
|
|
4
|
+
/**
|
|
5
|
+
* Creates image object, gets attributes from placeholder,
|
|
6
|
+
* sets them on image object, adds classes to image and
|
|
7
|
+
* when image is loaded, appends it to a placeholder
|
|
8
|
+
*/
|
|
9
|
+
function default_1(images, config) {
|
|
10
|
+
images.forEach(img => window['IntersectionObserver'] ? createObserver(img, config) : (0, imageLoader_1.loadImage)(img));
|
|
11
|
+
}
|
|
12
|
+
exports.default = default_1;
|
|
13
|
+
/**
|
|
14
|
+
* Creates instance of IntersectionObserver and loads image in DOM
|
|
15
|
+
* as soon as image will be visible on the screen
|
|
16
|
+
*/
|
|
17
|
+
function createObserver(lazyImage, config) {
|
|
18
|
+
const handleObserver = (entries, observer) => {
|
|
19
|
+
for (const entry of entries) {
|
|
20
|
+
if (!entry.isIntersecting) {
|
|
21
|
+
continue;
|
|
22
|
+
}
|
|
23
|
+
(0, imageLoader_1.loadImage)(lazyImage);
|
|
24
|
+
observer.unobserve(lazyImage);
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
const observer = new IntersectionObserver(handleObserver, config);
|
|
28
|
+
observer.observe(lazyImage);
|
|
29
|
+
}
|
|
30
|
+
//# sourceMappingURL=handler.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"handler.js","sourceRoot":"","sources":["../../src/handler.ts"],"names":[],"mappings":";;AACA,+CAAyC;AAEzC;;;;GAIG;AACH,mBAAyB,MAAmB,EAAE,MAAc;IACxD,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC,MAAM,CAAC,sBAAsB,CAAC,CAAC,CAAC,CAAC,cAAc,CAAC,GAAG,EAAE,MAAM,CAAC,CAAC,CAAC,CAAC,IAAA,uBAAS,EAAC,GAAG,CAAC,CAAC,CAAA;AACxG,CAAC;AAFD,4BAEC;AAED;;;GAGG;AACH,SAAS,cAAc,CAAC,SAAoB,EAAE,MAAc;IACxD,MAAM,cAAc,GAAG,CAAC,OAAoC,EAAE,QAA8B,EAAE,EAAE;QAC5F,KAAK,MAAM,KAAK,IAAI,OAAO,EAAE;YACzB,IAAI,CAAC,KAAK,CAAC,cAAc,EAAE;gBACvB,SAAQ;aACX;YAED,IAAA,uBAAS,EAAC,SAAS,CAAC,CAAA;YACpB,QAAQ,CAAC,SAAS,CAAC,SAAS,CAAC,CAAA;SAChC;IACL,CAAC,CAAA;IAED,MAAM,QAAQ,GAAG,IAAI,oBAAoB,CAAC,cAAc,EAAE,MAAM,CAAC,CAAA;IAEjE,QAAQ,CAAC,OAAO,CAAC,SAAS,CAAC,CAAA;AAC/B,CAAC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { LazyImage } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* If image has tag IMG then set the src attribute to img url,
|
|
4
|
+
* otherwise set the background of the element to given image url
|
|
5
|
+
*/
|
|
6
|
+
export declare function loadImage(lazyImage: LazyImage): void;
|
|
7
|
+
//# sourceMappingURL=imageLoader.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"imageLoader.d.ts","sourceRoot":"","sources":["../../src/imageLoader.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,SAAS,CAAA;AAEnC;;;GAGG;AACH,wBAAgB,SAAS,CAAC,SAAS,EAAE,SAAS,GAAG,IAAI,CA0BpD"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.loadImage = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* If image has tag IMG then set the src attribute to img url,
|
|
6
|
+
* otherwise set the background of the element to given image url
|
|
7
|
+
*/
|
|
8
|
+
function loadImage(lazyImage) {
|
|
9
|
+
const imageUrl = getImageUrl(lazyImage);
|
|
10
|
+
if (!imageUrl) {
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
if (!lazyImage.hasAttribute('src')) {
|
|
14
|
+
lazyImage.style.opacity = '0';
|
|
15
|
+
}
|
|
16
|
+
const ghostImage = new Image();
|
|
17
|
+
ghostImage.addEventListener('load', () => {
|
|
18
|
+
lazyImage.style.transition = 'opacity 777ms';
|
|
19
|
+
lazyImage.style.opacity = '1';
|
|
20
|
+
});
|
|
21
|
+
ghostImage.src = imageUrl;
|
|
22
|
+
if (isImageElement(lazyImage)) {
|
|
23
|
+
lazyImage.setAttribute('src', imageUrl);
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
lazyImage.style.backgroundImage = `url(${imageUrl})`;
|
|
27
|
+
}
|
|
28
|
+
exports.loadImage = loadImage;
|
|
29
|
+
const isImageElement = (img) => img.tagName === 'IMG';
|
|
30
|
+
const getImageUrl = (img) => img.getAttribute('data-src');
|
|
31
|
+
//# sourceMappingURL=imageLoader.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"imageLoader.js","sourceRoot":"","sources":["../../src/imageLoader.ts"],"names":[],"mappings":";;;AAEA;;;GAGG;AACH,SAAgB,SAAS,CAAC,SAAoB;IAC1C,MAAM,QAAQ,GAAG,WAAW,CAAC,SAAS,CAAC,CAAA;IAEvC,IAAI,CAAC,QAAQ,EAAE;QACX,OAAM;KACT;IAED,IAAI,CAAC,SAAS,CAAC,YAAY,CAAC,KAAK,CAAC,EAAE;QAChC,SAAS,CAAC,KAAK,CAAC,OAAO,GAAG,GAAG,CAAA;KAChC;IAED,MAAM,UAAU,GAAG,IAAI,KAAK,EAAE,CAAA;IAE9B,UAAU,CAAC,gBAAgB,CAAC,MAAM,EAAE,GAAG,EAAE;QACrC,SAAS,CAAC,KAAK,CAAC,UAAU,GAAG,eAAe,CAAA;QAC5C,SAAS,CAAC,KAAK,CAAC,OAAO,GAAG,GAAG,CAAA;IACjC,CAAC,CAAC,CAAA;IAEF,UAAU,CAAC,GAAG,GAAG,QAAQ,CAAA;IAEzB,IAAI,cAAc,CAAC,SAAS,CAAC,EAAE;QAC3B,SAAS,CAAC,YAAY,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAA;QACvC,OAAM;KACT;IAED,SAAS,CAAC,KAAK,CAAC,eAAe,GAAG,OAAO,QAAQ,GAAG,CAAA;AACxD,CAAC;AA1BD,8BA0BC;AAED,MAAM,cAAc,GAAG,CAAC,GAAc,EAAE,EAAE,CAAC,GAAG,CAAC,OAAO,KAAK,KAAK,CAAA;AAChE,MAAM,WAAW,GAAG,CAAC,GAAc,EAAE,EAAE,CAAC,GAAG,CAAC,YAAY,CAAC,UAAU,CAAC,CAAA"}
|
package/lib/js/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Config,
|
|
2
|
-
declare const _default: (selector?:
|
|
1
|
+
import { Config, InputTypes } from './types';
|
|
2
|
+
declare const _default: (selector?: InputTypes, config?: Config) => void;
|
|
3
3
|
export default _default;
|
|
4
4
|
//# sourceMappingURL=index.d.ts.map
|
package/lib/js/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,EAAa,UAAU,EAAE,MAAM,SAAS,CAAA;oCAE5B,UAAU,WAAW,MAAM,KAAG,IAAI;AAA7D,wBAaC"}
|
package/lib/js/index.js
CHANGED
|
@@ -3,22 +3,30 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
const
|
|
6
|
+
const handler_1 = __importDefault(require("./handler"));
|
|
7
7
|
exports.default = (selector, config) => {
|
|
8
8
|
const images = getImagesElements(selector);
|
|
9
|
-
if (!images)
|
|
9
|
+
if (!images) {
|
|
10
10
|
return;
|
|
11
|
+
}
|
|
11
12
|
const configurations = config || {
|
|
12
13
|
root: null,
|
|
13
14
|
threshold: 0,
|
|
14
15
|
};
|
|
15
|
-
return
|
|
16
|
+
return (0, handler_1.default)(images, configurations);
|
|
16
17
|
};
|
|
17
18
|
function getImagesElements(selector) {
|
|
18
|
-
if (
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
19
|
+
if (!selector) {
|
|
20
|
+
const elements = document.querySelectorAll('.smooth-loader[data-src]');
|
|
21
|
+
return Array.from(elements);
|
|
22
|
+
}
|
|
23
|
+
if (typeof selector === 'string') {
|
|
24
|
+
const elements = document.querySelectorAll(selector);
|
|
25
|
+
return Array.from(elements);
|
|
26
|
+
}
|
|
27
|
+
if (selector instanceof NodeList) {
|
|
28
|
+
return Array.from(selector);
|
|
29
|
+
}
|
|
30
|
+
return selector;
|
|
23
31
|
}
|
|
24
32
|
//# sourceMappingURL=index.js.map
|
package/lib/js/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;AAAA,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;AAAA,wDAA+B;AAG/B,kBAAe,CAAC,QAAqB,EAAE,MAAe,EAAQ,EAAE;IAC5D,MAAM,MAAM,GAAG,iBAAiB,CAAC,QAAQ,CAAC,CAAA;IAE1C,IAAI,CAAC,MAAM,EAAE;QACT,OAAM;KACT;IAED,MAAM,cAAc,GAAG,MAAM,IAAI;QAC7B,IAAI,EAAE,IAAI;QACV,SAAS,EAAE,CAAC;KACf,CAAA;IAED,OAAO,IAAA,iBAAO,EAAC,MAAM,EAAE,cAAc,CAAC,CAAA;AAC1C,CAAC,CAAA;AAED,SAAS,iBAAiB,CAAC,QAAqB;IAC5C,IAAI,CAAC,QAAQ,EAAE;QACX,MAAM,QAAQ,GAAG,QAAQ,CAAC,gBAAgB,CAAY,0BAA0B,CAAC,CAAA;QACjF,OAAO,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAA;KAC9B;IAED,IAAI,OAAO,QAAQ,KAAK,QAAQ,EAAE;QAC9B,MAAM,QAAQ,GAAG,QAAQ,CAAC,gBAAgB,CAAY,QAAQ,CAAC,CAAA;QAC/D,OAAO,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAA;KAC9B;IAED,IAAI,QAAQ,YAAY,QAAQ,EAAE;QAC9B,OAAO,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAA;KAC9B;IAED,OAAO,QAAQ,CAAA;AACnB,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { Config, LazyImage } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Creates image object, gets attributes from placeholder,
|
|
4
|
+
* sets them on image object, adds classes to image and
|
|
5
|
+
* when image is loaded, appends it to a placeholder
|
|
6
|
+
*/
|
|
7
|
+
export default function (images: NodeListOf<LazyImage>, config: Config): void;
|
|
8
|
+
//# sourceMappingURL=loader.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"loader.d.ts","sourceRoot":"","sources":["../../src/loader.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,SAAS,CAAA;AAE3C;;;;GAIG;AACH,MAAM,CAAC,OAAO,WAAW,MAAM,EAAE,UAAU,CAAC,SAAS,CAAC,EAAE,MAAM,EAAE,MAAM,GAAG,IAAI,CAI5E"}
|
package/lib/js/loader.js
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
/**
|
|
4
|
+
* Creates image object, gets attributes from placeholder,
|
|
5
|
+
* sets them on image object, adds classes to image and
|
|
6
|
+
* when image is loaded, appends it to a placeholder
|
|
7
|
+
*/
|
|
8
|
+
function default_1(images, config) {
|
|
9
|
+
for (const img of images) {
|
|
10
|
+
window['IntersectionObserver'] ? createObserver(img, config) : loadImage(img);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
exports.default = default_1;
|
|
14
|
+
/**
|
|
15
|
+
* If image has tag IMG then set the src attribute to img url,
|
|
16
|
+
* otherwise set the background of the element to given image url
|
|
17
|
+
*/
|
|
18
|
+
function loadImage(lazyImage) {
|
|
19
|
+
const imageUrl = lazyImage.getAttribute('data-src');
|
|
20
|
+
if (!imageUrl) {
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
if (!lazyImage.hasAttribute('src')) {
|
|
24
|
+
lazyImage.style.opacity = '0';
|
|
25
|
+
}
|
|
26
|
+
const ghostImage = new Image();
|
|
27
|
+
ghostImage.addEventListener('load', () => {
|
|
28
|
+
lazyImage.style.transition = 'opacity 777ms';
|
|
29
|
+
lazyImage.style.opacity = '1';
|
|
30
|
+
});
|
|
31
|
+
ghostImage.src = imageUrl;
|
|
32
|
+
if (lazyImage.tagName === 'IMG') {
|
|
33
|
+
lazyImage.setAttribute('src', imageUrl);
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
lazyImage.style.backgroundImage = `url(${imageUrl})`;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Creates instance of IntersectionObserver and loads image in DOM
|
|
40
|
+
* as soon as image will be visible on the screen
|
|
41
|
+
*/
|
|
42
|
+
function createObserver(lazyImage, config) {
|
|
43
|
+
const handleObserver = (entries, observer) => {
|
|
44
|
+
for (const entry of entries) {
|
|
45
|
+
if (!entry.isIntersecting) {
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
loadImage(lazyImage);
|
|
49
|
+
observer.unobserve(lazyImage);
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
const observer = new IntersectionObserver(handleObserver, config);
|
|
53
|
+
observer.observe(lazyImage);
|
|
54
|
+
}
|
|
55
|
+
//# sourceMappingURL=loader.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"loader.js","sourceRoot":"","sources":["../../src/loader.ts"],"names":[],"mappings":";;AAEA;;;;GAIG;AACH,mBAAyB,MAA6B,EAAE,MAAc;IAClE,KAAK,MAAM,GAAG,IAAI,MAAM,EAAE;QACtB,MAAM,CAAC,sBAAsB,CAAC,CAAC,CAAC,CAAC,cAAc,CAAC,GAAG,EAAE,MAAM,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,GAAG,CAAC,CAAA;KAChF;AACL,CAAC;AAJD,4BAIC;AAED;;;GAGG;AACH,SAAS,SAAS,CAAC,SAAoB;IACnC,MAAM,QAAQ,GAAG,SAAS,CAAC,YAAY,CAAC,UAAU,CAAC,CAAA;IAEnD,IAAI,CAAC,QAAQ,EAAE;QACX,OAAM;KACT;IAED,IAAI,CAAC,SAAS,CAAC,YAAY,CAAC,KAAK,CAAC,EAAE;QAChC,SAAS,CAAC,KAAK,CAAC,OAAO,GAAG,GAAG,CAAA;KAChC;IAED,MAAM,UAAU,GAAG,IAAI,KAAK,EAAE,CAAA;IAE9B,UAAU,CAAC,gBAAgB,CAAC,MAAM,EAAE,GAAG,EAAE;QACrC,SAAS,CAAC,KAAK,CAAC,UAAU,GAAG,eAAe,CAAA;QAC5C,SAAS,CAAC,KAAK,CAAC,OAAO,GAAG,GAAG,CAAA;IACjC,CAAC,CAAC,CAAA;IAEF,UAAU,CAAC,GAAG,GAAG,QAAQ,CAAA;IAEzB,IAAI,SAAS,CAAC,OAAO,KAAK,KAAK,EAAE;QAC7B,SAAS,CAAC,YAAY,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAA;QACvC,OAAM;KACT;IAED,SAAS,CAAC,KAAK,CAAC,eAAe,GAAG,OAAO,QAAQ,GAAG,CAAA;AACxD,CAAC;AAGD;;;GAGG;AACH,SAAS,cAAc,CAAC,SAAoB,EAAE,MAAc;IACxD,MAAM,cAAc,GAAG,CAAC,OAAoC,EAAE,QAA8B,EAAE,EAAE;QAC5F,KAAK,MAAM,KAAK,IAAI,OAAO,EAAE;YACzB,IAAI,CAAC,KAAK,CAAC,cAAc,EAAE;gBACvB,SAAQ;aACX;YAED,SAAS,CAAC,SAAS,CAAC,CAAA;YACpB,QAAQ,CAAC,SAAS,CAAC,SAAS,CAAC,CAAA;SAChC;IACL,CAAC,CAAA;IAED,MAAM,QAAQ,GAAG,IAAI,oBAAoB,CAAC,cAAc,EAAE,MAAM,CAAC,CAAA;IAEjE,QAAQ,CAAC,OAAO,CAAC,SAAS,CAAC,CAAA;AAC/B,CAAC"}
|
package/lib/js/types.d.ts
CHANGED
package/lib/js/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"AAAA,oBAAY,SAAS,GAAG,cAAc,GAAG,gBAAgB,CAAA;
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"AAAA,oBAAY,SAAS,GAAG,cAAc,GAAG,gBAAgB,CAAA;AACzD,oBAAY,UAAU,GAAG,UAAU,CAAC,SAAS,CAAC,GAAG,SAAS,EAAE,GAAG,MAAM,CAAA;AAErE,MAAM,WAAW,MAAO,SAAQ,wBAAwB;CAAI"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "smooth-loader",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.2",
|
|
4
4
|
"description": "This package allows you to lazy load your images using IntersectionObserver technology. It supports loading background images as well as the regular images with img tag.",
|
|
5
5
|
"main": "lib/js/index.js",
|
|
6
6
|
"types": "lib/js",
|
package/src/handler.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { Config, LazyImage } from './types'
|
|
2
|
+
import { loadImage } from './imageLoader'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Creates image object, gets attributes from placeholder,
|
|
6
|
+
* sets them on image object, adds classes to image and
|
|
7
|
+
* when image is loaded, appends it to a placeholder
|
|
8
|
+
*/
|
|
9
|
+
export default function (images: LazyImage[], config: Config): void {
|
|
10
|
+
images.forEach(img => window['IntersectionObserver'] ? createObserver(img, config) : loadImage(img))
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Creates instance of IntersectionObserver and loads image in DOM
|
|
15
|
+
* as soon as image will be visible on the screen
|
|
16
|
+
*/
|
|
17
|
+
function createObserver(lazyImage: LazyImage, config: Config): void {
|
|
18
|
+
const handleObserver = (entries: IntersectionObserverEntry[], observer: IntersectionObserver) => {
|
|
19
|
+
for (const entry of entries) {
|
|
20
|
+
if (!entry.isIntersecting) {
|
|
21
|
+
continue
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
loadImage(lazyImage)
|
|
25
|
+
observer.unobserve(lazyImage)
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const observer = new IntersectionObserver(handleObserver, config)
|
|
30
|
+
|
|
31
|
+
observer.observe(lazyImage)
|
|
32
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { LazyImage } from './types'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* If image has tag IMG then set the src attribute to img url,
|
|
5
|
+
* otherwise set the background of the element to given image url
|
|
6
|
+
*/
|
|
7
|
+
export function loadImage(lazyImage: LazyImage): void {
|
|
8
|
+
const imageUrl = getImageUrl(lazyImage)
|
|
9
|
+
|
|
10
|
+
if (!imageUrl) {
|
|
11
|
+
return
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
if (!lazyImage.hasAttribute('src')) {
|
|
15
|
+
lazyImage.style.opacity = '0'
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const ghostImage = new Image()
|
|
19
|
+
|
|
20
|
+
ghostImage.addEventListener('load', () => {
|
|
21
|
+
lazyImage.style.transition = 'opacity 777ms'
|
|
22
|
+
lazyImage.style.opacity = '1'
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
ghostImage.src = imageUrl
|
|
26
|
+
|
|
27
|
+
if (isImageElement(lazyImage)) {
|
|
28
|
+
lazyImage.setAttribute('src', imageUrl)
|
|
29
|
+
return
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
lazyImage.style.backgroundImage = `url(${imageUrl})`
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const isImageElement = (img: LazyImage) => img.tagName === 'IMG'
|
|
36
|
+
const getImageUrl = (img: LazyImage) => img.getAttribute('data-src')
|
package/src/index.ts
CHANGED
|
@@ -1,26 +1,35 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { Config, LazyImage } from './types'
|
|
1
|
+
import handler from './handler'
|
|
2
|
+
import { Config, LazyImage, InputTypes } from './types'
|
|
3
3
|
|
|
4
|
-
export default (selector?:
|
|
4
|
+
export default (selector?: InputTypes, config?: Config): void => {
|
|
5
5
|
const images = getImagesElements(selector)
|
|
6
6
|
|
|
7
|
-
if (!images)
|
|
7
|
+
if (!images) {
|
|
8
8
|
return
|
|
9
|
+
}
|
|
9
10
|
|
|
10
11
|
const configurations = config || {
|
|
11
12
|
root: null,
|
|
12
13
|
threshold: 0,
|
|
13
14
|
}
|
|
14
15
|
|
|
15
|
-
return
|
|
16
|
+
return handler(images, configurations)
|
|
16
17
|
}
|
|
17
18
|
|
|
18
|
-
function getImagesElements(selector?:
|
|
19
|
-
if (
|
|
20
|
-
|
|
19
|
+
function getImagesElements(selector?: InputTypes): LazyImage[] {
|
|
20
|
+
if (!selector) {
|
|
21
|
+
const elements = document.querySelectorAll<LazyImage>('.smooth-loader[data-src]')
|
|
22
|
+
return Array.from(elements)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if (typeof selector === 'string') {
|
|
26
|
+
const elements = document.querySelectorAll<LazyImage>(selector)
|
|
27
|
+
return Array.from(elements)
|
|
28
|
+
}
|
|
21
29
|
|
|
22
|
-
if (selector instanceof NodeList)
|
|
23
|
-
return selector
|
|
30
|
+
if (selector instanceof NodeList) {
|
|
31
|
+
return Array.from(selector)
|
|
32
|
+
}
|
|
24
33
|
|
|
25
|
-
return
|
|
26
|
-
}
|
|
34
|
+
return selector
|
|
35
|
+
}
|
package/src/types.ts
CHANGED
package/src/LazyLoader.ts
DELETED
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
import { Config, LazyImage } from './types'
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Class that handles lazy loading images with
|
|
5
|
-
* Intersection Observer
|
|
6
|
-
*/
|
|
7
|
-
export default class {
|
|
8
|
-
public constructor(
|
|
9
|
-
private readonly images: NodeListOf<LazyImage>,
|
|
10
|
-
private readonly config: Config
|
|
11
|
-
) {
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* If image has tag IMG then set the src attribute to img url,
|
|
16
|
-
* otherwise set the background of the element to given image url
|
|
17
|
-
*/
|
|
18
|
-
private loadImage(lazyImage: LazyImage): void {
|
|
19
|
-
const imageUrl = lazyImage.getAttribute('data-src')
|
|
20
|
-
|
|
21
|
-
if (!imageUrl) {
|
|
22
|
-
return
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
if (!lazyImage.hasAttribute('src')) {
|
|
26
|
-
lazyImage.style.opacity = '0'
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
const ghostImage = new Image()
|
|
30
|
-
|
|
31
|
-
ghostImage.addEventListener('load', () => {
|
|
32
|
-
lazyImage.style.transition = 'opacity 777ms'
|
|
33
|
-
lazyImage.style.opacity = '1'
|
|
34
|
-
})
|
|
35
|
-
|
|
36
|
-
ghostImage.src = imageUrl
|
|
37
|
-
|
|
38
|
-
if (lazyImage.tagName === 'IMG') {
|
|
39
|
-
lazyImage.setAttribute('src', imageUrl)
|
|
40
|
-
return
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
lazyImage.style.backgroundImage = `url(${imageUrl})`
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Creates instance of IntersectionObserver and loads image in DOM
|
|
49
|
-
* as soon as image will be visible on the screen
|
|
50
|
-
*/
|
|
51
|
-
private createObserver(lazyImage: LazyImage): void {
|
|
52
|
-
const handleObserver = (entries: IntersectionObserverEntry[], observer: IntersectionObserver) => {
|
|
53
|
-
for (const entry of entries) {
|
|
54
|
-
if (!entry.isIntersecting) {
|
|
55
|
-
continue
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
this.loadImage(lazyImage)
|
|
59
|
-
observer.unobserve(lazyImage)
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
const observer = new IntersectionObserver(handleObserver, this.config)
|
|
64
|
-
|
|
65
|
-
observer.observe(lazyImage)
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* Creates image object, gets attributes from placeholder,
|
|
70
|
-
* sets them on image object, adds classes to image and
|
|
71
|
-
* when image is loaded, appends it to a placeholder
|
|
72
|
-
*/
|
|
73
|
-
public execute(): void {
|
|
74
|
-
for (const img of this.images) {
|
|
75
|
-
window['IntersectionObserver']
|
|
76
|
-
? this.createObserver(img)
|
|
77
|
-
: this.loadImage(img)
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
}
|