smooth-loader 1.0.4 → 1.0.7
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 +16 -0
- package/README.md +32 -31
- package/lib/js/LazyLoader.d.ts +3 -2
- package/lib/js/LazyLoader.d.ts.map +1 -1
- package/lib/js/LazyLoader.js +13 -10
- package/lib/js/LazyLoader.js.map +1 -1
- package/lib/js/index.js +1 -1
- package/lib/js/index.js.map +1 -1
- package/package.json +10 -3
- package/src/LazyLoader.ts +25 -19
- package/src/index.ts +9 -4
- package/src/types.ts +4 -1
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# Release Notes
|
|
2
|
+
|
|
3
|
+
## v1.0.7 (2022-06-22)
|
|
4
|
+
|
|
5
|
+
- Fixed appearing transition with background images. Now it works.
|
|
6
|
+
- Improved documentation `README.md` file.
|
|
7
|
+
- Added **configurations** for smooth loading images.
|
|
8
|
+
|
|
9
|
+
----
|
|
10
|
+
|
|
11
|
+
## v1.0.6 (2022-01-2)
|
|
12
|
+
|
|
13
|
+
- Changed default images selector to be more specific.
|
|
14
|
+
- Added `CHANGELOG.md` file.
|
|
15
|
+
|
|
16
|
+
----
|
package/README.md
CHANGED
|
@@ -1,43 +1,34 @@
|
|
|
1
|
-
|
|
1
|
+
- Documentation menu
|
|
2
|
+
- [📄 Description](#-description)
|
|
3
|
+
- [🎁 Features](#-features)
|
|
4
|
+
- [✍️ Usage](#%EF%B8%8F-usage)
|
|
5
|
+
- [🚀️ Quick start](#-quick-start)
|
|
6
|
+
- Useful links
|
|
7
|
+
- [NPM](https://www.npmjs.com/package/smooth-loader)
|
|
8
|
+
- [Usage on codesandbox](https://codesandbox.io/s/smooth-loader-example-usage-5xr6h)
|
|
9
|
+
- [Example usage in production](https://shobar.com.ua/category/photo-reports)
|
|
10
|
+
|
|
11
|
+
## 📄 Description
|
|
2
12
|
|
|
3
13
|
Smooth loader allows you smoothly lazy load images and background images. Lazy Loading defers the loading of an image that is not needed on the page immediately. An image, not visible to the user when the page loads, is loaded later when the user scrolls and the image actually becomes visible. If the user never scrolls, an image that is not visible to the user never gets loaded.
|
|
4
14
|
|
|
5
|
-
-
|
|
6
|
-
- [Usage on codesandbox](https://codesandbox.io/s/smooth-loader-example-usage-5xr6h)
|
|
7
|
-
- [Example usage in production](https://shobar.com.ua/category/photo-reports)
|
|
15
|
+
This package is a MUST-HAVE for every website containing multiple images on the page. It's surely the best lazy loading package on NPM that you can configure to your own needs.
|
|
8
16
|
|
|
9
|
-
|
|
17
|
+
## 🎁 Features
|
|
10
18
|
|
|
11
|
-
- Written in TypeScript
|
|
12
|
-
- Very small package
|
|
13
|
-
- Loads images with IMG tag and backgrounds
|
|
14
|
-
- Loads images with a smooth fade in animation
|
|
19
|
+
- ✅ Written in TypeScript;
|
|
20
|
+
- ✅ Very small package;
|
|
21
|
+
- ✅ Loads images with IMG tag and backgrounds;
|
|
22
|
+
- ✅ Loads images with a smooth fade in animation;
|
|
23
|
+
- ✅ Configurable;
|
|
15
24
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
```bash
|
|
19
|
-
# npm
|
|
20
|
-
npm i smooth-loader
|
|
21
|
-
|
|
22
|
-
# yarn
|
|
23
|
-
yarn add smooth-loader
|
|
24
|
-
```
|
|
25
|
-
|
|
26
|
-
Import `smooth-loader` and the core CSS. This assumes you're using a module bundler like webpack.
|
|
27
|
-
|
|
28
|
-
```js
|
|
29
|
-
import smoothLoader from 'smooth-loader'
|
|
30
|
-
import 'smooth-loader/lib/css/index.css'
|
|
31
|
-
```
|
|
32
|
-
|
|
33
|
-
# Usage
|
|
25
|
+
## ✍️ Usage
|
|
34
26
|
|
|
35
27
|
To make image or element Lazy loaded, replace `src` attribute with `data-src`
|
|
36
28
|
on image element, and add `data-src` attribute to elements that have background
|
|
37
29
|
image that you want to lazy load.
|
|
38
30
|
|
|
39
|
-
- Add **smooth-loader** css class to each image or element that you want
|
|
40
|
-
Unless you want to specify your own selector for images instead of **smooth-loader**.
|
|
31
|
+
- Add **smooth-loader** css class to each image or element that you want or specify your own selector for images instead of **smooth-loader**.
|
|
41
32
|
- Call `smoothLoader()` function when you want to lazy load images.
|
|
42
33
|
|
|
43
34
|
```js
|
|
@@ -62,8 +53,18 @@ smoothLoader(myImages)
|
|
|
62
53
|
<div data-src="./images/we.png" class="smooth-loader my-bg-image"></div>
|
|
63
54
|
```
|
|
64
55
|
|
|
65
|
-
|
|
56
|
+
## 🚀 Quick Start
|
|
57
|
+
|
|
58
|
+
```bash
|
|
59
|
+
# npm
|
|
60
|
+
npm i smooth-loader
|
|
61
|
+
|
|
62
|
+
# yarn
|
|
63
|
+
yarn add smooth-loader
|
|
64
|
+
```
|
|
66
65
|
|
|
67
66
|
```js
|
|
68
|
-
|
|
67
|
+
import smoothLoader from 'smooth-loader'
|
|
68
|
+
|
|
69
|
+
smoothLoader()
|
|
69
70
|
```
|
package/lib/js/LazyLoader.d.ts
CHANGED
|
@@ -5,7 +5,7 @@ import { LazyImage } from './types';
|
|
|
5
5
|
*/
|
|
6
6
|
export default class {
|
|
7
7
|
private readonly images;
|
|
8
|
-
private observerOptions;
|
|
8
|
+
private readonly observerOptions;
|
|
9
9
|
constructor(images: NodeListOf<LazyImage>);
|
|
10
10
|
/**
|
|
11
11
|
* If image has tag IMG then set the src attribute to img url,
|
|
@@ -13,7 +13,8 @@ export default class {
|
|
|
13
13
|
*/
|
|
14
14
|
private loadImage;
|
|
15
15
|
/**
|
|
16
|
-
* Create observer object
|
|
16
|
+
* Create observer object that will trigger loading image function
|
|
17
|
+
* when it's gonna be visible on the screen
|
|
17
18
|
*/
|
|
18
19
|
private createObserver;
|
|
19
20
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"LazyLoader.d.ts","sourceRoot":"","sources":["../../src/LazyLoader.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,SAAS,CAAA;AAEnC;;;GAGG;AACH,MAAM,CAAC,OAAO;
|
|
1
|
+
{"version":3,"file":"LazyLoader.d.ts","sourceRoot":"","sources":["../../src/LazyLoader.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,SAAS,CAAA;AAEnC;;;GAGG;AACH,MAAM,CAAC,OAAO;IAMS,OAAO,CAAC,QAAQ,CAAC,MAAM;IAL1C,OAAO,CAAC,QAAQ,CAAC,eAAe,CAG/B;gBAEmC,MAAM,EAAE,UAAU,CAAC,SAAS,CAAC;IAGjE;;;OAGG;IACH,OAAO,CAAC,SAAS;IAuBjB;;;OAGG;IACH,OAAO,CAAC,cAAc;IAatB;;;;OAIG;IACI,OAAO,IAAI,IAAI;CAKzB"}
|
package/lib/js/LazyLoader.js
CHANGED
|
@@ -6,11 +6,11 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
6
6
|
*/
|
|
7
7
|
class default_1 {
|
|
8
8
|
constructor(images) {
|
|
9
|
+
this.images = images;
|
|
9
10
|
this.observerOptions = {
|
|
10
11
|
root: null,
|
|
11
12
|
threshold: 0,
|
|
12
13
|
};
|
|
13
|
-
this.images = images;
|
|
14
14
|
}
|
|
15
15
|
/**
|
|
16
16
|
* If image has tag IMG then set the src attribute to img url,
|
|
@@ -20,17 +20,20 @@ class default_1 {
|
|
|
20
20
|
const imageUrl = img.getAttribute('data-src');
|
|
21
21
|
if (!imageUrl)
|
|
22
22
|
return;
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
23
|
+
img.style.opacity = '0';
|
|
24
|
+
const ghostImage = new Image();
|
|
25
|
+
ghostImage.addEventListener('load', () => {
|
|
26
|
+
img.style.transition = 'opacity 777ms';
|
|
27
|
+
img.style.opacity = '1';
|
|
28
|
+
});
|
|
29
|
+
ghostImage.src = imageUrl;
|
|
30
|
+
img.tagName === 'IMG'
|
|
31
|
+
? img.setAttribute('src', imageUrl)
|
|
32
|
+
: img.style.backgroundImage = `url(${imageUrl})`;
|
|
31
33
|
}
|
|
32
34
|
/**
|
|
33
|
-
* Create observer object
|
|
35
|
+
* Create observer object that will trigger loading image function
|
|
36
|
+
* when it's gonna be visible on the screen
|
|
34
37
|
*/
|
|
35
38
|
createObserver(img) {
|
|
36
39
|
const observer = new IntersectionObserver((entries, observer) => {
|
package/lib/js/LazyLoader.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"LazyLoader.js","sourceRoot":"","sources":["../../src/LazyLoader.ts"],"names":[],"mappings":";;AAEA;;;GAGG;AACH;
|
|
1
|
+
{"version":3,"file":"LazyLoader.js","sourceRoot":"","sources":["../../src/LazyLoader.ts"],"names":[],"mappings":";;AAEA;;;GAGG;AACH;IAMI,YAAoC,MAA6B;QAA7B,WAAM,GAAN,MAAM,CAAuB;QALhD,oBAAe,GAAG;YAC/B,IAAI,EAAE,IAAI;YACV,SAAS,EAAE,CAAC;SACf,CAAA;IAGD,CAAC;IAED;;;OAGG;IACK,SAAS,CAAC,GAAc;QAC5B,MAAM,QAAQ,GAAG,GAAG,CAAC,YAAY,CAAC,UAAU,CAAC,CAAA;QAE7C,IAAI,CAAC,QAAQ;YACT,OAAM;QAEV,GAAG,CAAC,KAAK,CAAC,OAAO,GAAG,GAAG,CAAA;QAEvB,MAAM,UAAU,GAAG,IAAI,KAAK,EAAE,CAAA;QAE9B,UAAU,CAAC,gBAAgB,CAAC,MAAM,EAAE,GAAG,EAAE;YACrC,GAAG,CAAC,KAAK,CAAC,UAAU,GAAG,eAAe,CAAA;YACtC,GAAG,CAAC,KAAK,CAAC,OAAO,GAAG,GAAG,CAAA;QAC3B,CAAC,CAAC,CAAA;QAEF,UAAU,CAAC,GAAG,GAAG,QAAQ,CAAA;QAEzB,GAAG,CAAC,OAAO,KAAK,KAAK;YACjB,CAAC,CAAC,GAAG,CAAC,YAAY,CAAC,KAAK,EAAE,QAAQ,CAAC;YACnC,CAAC,CAAC,GAAG,CAAC,KAAK,CAAC,eAAe,GAAG,OAAO,QAAQ,GAAG,CAAA;IACxD,CAAC;IAGD;;;OAGG;IACK,cAAc,CAAC,GAAc;QACjC,MAAM,QAAQ,GAAG,IAAI,oBAAoB,CAAC,CAAC,OAAO,EAAE,QAAQ,EAAE,EAAE;YAC5D,OAAO,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE;gBACpB,IAAI,KAAK,CAAC,cAAc,EAAE;oBACtB,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,CAAA;oBACnB,QAAQ,CAAC,SAAS,CAAC,GAAG,CAAC,CAAA;iBAC1B;YACL,CAAC,CAAC,CAAA;QACN,CAAC,EAAE,IAAI,CAAC,eAAe,CAAC,CAAA;QAExB,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,CAAA;IACzB,CAAC;IAED;;;;OAIG;IACI,OAAO;QACV,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE;YACtB,CAAC,MAAM,CAAC,sBAAsB,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,cAAc,CAAC,GAAG,CAAC,CAAA;QACpF,CAAC,CAAC,CAAA;IACN,CAAC;CACJ;AA/DD,4BA+DC"}
|
package/lib/js/index.js
CHANGED
|
@@ -15,6 +15,6 @@ function getImagesElements(selector) {
|
|
|
15
15
|
return document.querySelectorAll(selector);
|
|
16
16
|
if (selector instanceof NodeList)
|
|
17
17
|
return selector;
|
|
18
|
-
return document.querySelectorAll('.smooth-loader');
|
|
18
|
+
return document.querySelectorAll('.smooth-loader[data-src]');
|
|
19
19
|
}
|
|
20
20
|
//# 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,8DAAqC;AAGrC,kBAAe,CAAC,QAAyC,EAAQ,EAAE;IAC/D,MAAM,MAAM,GAAG,iBAAiB,CAAC,QAAQ,CAAC,CAAA;IAE1C,IAAI,CAAC,MAAM;QACP,OAAM;IAEV,OAAO,IAAI,oBAAU,CAAC,MAAM,CAAC,CAAC,OAAO,EAAE,CAAA;AAC3C,CAAC,CAAA;AAED,SAAS,iBAAiB,CAAC,QAAyC;IAChE,IAAI,OAAO,QAAQ,KAAK,QAAQ,IAAI,QAAQ,KAAK,EAAE;QAC/C,OAAO,QAAQ,CAAC,gBAAgB,CAAY,QAAQ,CAAC,CAAA;IAEzD,IAAI,QAAQ,YAAY,QAAQ;QAC5B,OAAO,QAAQ,CAAA;IAEnB,OAAO,QAAQ,CAAC,gBAAgB,CAAY,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;AAAA,8DAAqC;AAGrC,kBAAe,CAAC,QAAyC,EAAQ,EAAE;IAC/D,MAAM,MAAM,GAAG,iBAAiB,CAAC,QAAQ,CAAC,CAAA;IAE1C,IAAI,CAAC,MAAM;QACP,OAAM;IAEV,OAAO,IAAI,oBAAU,CAAC,MAAM,CAAC,CAAC,OAAO,EAAE,CAAA;AAC3C,CAAC,CAAA;AAED,SAAS,iBAAiB,CAAC,QAAyC;IAChE,IAAI,OAAO,QAAQ,KAAK,QAAQ,IAAI,QAAQ,KAAK,EAAE;QAC/C,OAAO,QAAQ,CAAC,gBAAgB,CAAY,QAAQ,CAAC,CAAA;IAEzD,IAAI,QAAQ,YAAY,QAAQ;QAC5B,OAAO,QAAQ,CAAA;IAEnB,OAAO,QAAQ,CAAC,gBAAgB,CAAY,0BAA0B,CAAC,CAAA;AAC3E,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "smooth-loader",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.7",
|
|
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",
|
|
@@ -9,9 +9,16 @@
|
|
|
9
9
|
},
|
|
10
10
|
"scripts": {
|
|
11
11
|
"build": "tsc -p .",
|
|
12
|
-
"watch": "tsc --watch"
|
|
12
|
+
"watch": "tsc --watch",
|
|
13
|
+
"publish": "npm login && npm publish"
|
|
13
14
|
},
|
|
14
|
-
"keywords": [
|
|
15
|
+
"keywords": [
|
|
16
|
+
"lazy loader",
|
|
17
|
+
"lazy",
|
|
18
|
+
"image loader",
|
|
19
|
+
"lazy loading",
|
|
20
|
+
"background image"
|
|
21
|
+
],
|
|
15
22
|
"author": "Serhii Cho",
|
|
16
23
|
"license": "MIT",
|
|
17
24
|
"homepage": "https://github.com/SerhiiCho/smooth-loader#readme"
|
package/src/LazyLoader.ts
CHANGED
|
@@ -1,18 +1,14 @@
|
|
|
1
|
-
import { LazyImage } from './types'
|
|
1
|
+
import { Config, LazyImage } from './types'
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Class that handles lazy loading images with
|
|
5
5
|
* Intersection Observer
|
|
6
6
|
*/
|
|
7
7
|
export default class {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
public constructor(images: NodeListOf<LazyImage>) {
|
|
15
|
-
this.images = images
|
|
8
|
+
public constructor(
|
|
9
|
+
private readonly images: NodeListOf<LazyImage>,
|
|
10
|
+
private readonly config: Config
|
|
11
|
+
) {
|
|
16
12
|
}
|
|
17
13
|
|
|
18
14
|
/**
|
|
@@ -22,19 +18,29 @@ export default class {
|
|
|
22
18
|
private loadImage(img: LazyImage): void {
|
|
23
19
|
const imageUrl = img.getAttribute('data-src')
|
|
24
20
|
|
|
25
|
-
if (!imageUrl)
|
|
21
|
+
if (!imageUrl)
|
|
22
|
+
return
|
|
23
|
+
|
|
24
|
+
img.style.opacity = '0'
|
|
25
|
+
|
|
26
|
+
const ghostImage = new Image()
|
|
26
27
|
|
|
27
|
-
|
|
28
|
-
img.
|
|
29
|
-
img.
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
28
|
+
ghostImage.addEventListener('load', () => {
|
|
29
|
+
img.style.transition = 'opacity 777ms'
|
|
30
|
+
img.style.opacity = '1'
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
ghostImage.src = imageUrl
|
|
34
|
+
|
|
35
|
+
img.tagName === 'IMG'
|
|
36
|
+
? img.setAttribute('src', imageUrl)
|
|
37
|
+
: img.style.backgroundImage = `url(${imageUrl})`
|
|
34
38
|
}
|
|
35
39
|
|
|
40
|
+
|
|
36
41
|
/**
|
|
37
|
-
* Create observer object
|
|
42
|
+
* Create observer object that will trigger loading image function
|
|
43
|
+
* when it's gonna be visible on the screen
|
|
38
44
|
*/
|
|
39
45
|
private createObserver(img: LazyImage): void {
|
|
40
46
|
const observer = new IntersectionObserver((entries, observer) => {
|
|
@@ -44,7 +50,7 @@ export default class {
|
|
|
44
50
|
observer.unobserve(img)
|
|
45
51
|
}
|
|
46
52
|
})
|
|
47
|
-
}, this.
|
|
53
|
+
}, this.config)
|
|
48
54
|
|
|
49
55
|
observer.observe(img)
|
|
50
56
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,13 +1,18 @@
|
|
|
1
1
|
import LazyLoader from "./LazyLoader"
|
|
2
|
-
import { LazyImage } from './types'
|
|
2
|
+
import { Config, LazyImage } from './types'
|
|
3
3
|
|
|
4
|
-
export default (selector?: NodeListOf<LazyImage> | string): void => {
|
|
4
|
+
export default (selector?: NodeListOf<LazyImage> | string, config?: Config): void => {
|
|
5
5
|
const images = getImagesElements(selector)
|
|
6
6
|
|
|
7
7
|
if (!images)
|
|
8
8
|
return
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
const configurations = config || {
|
|
11
|
+
root: null,
|
|
12
|
+
threshold: 0,
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
return new LazyLoader(images, configurations).execute()
|
|
11
16
|
}
|
|
12
17
|
|
|
13
18
|
function getImagesElements(selector?: NodeListOf<LazyImage> | string): NodeListOf<LazyImage> {
|
|
@@ -17,5 +22,5 @@ function getImagesElements(selector?: NodeListOf<LazyImage> | string): NodeListO
|
|
|
17
22
|
if (selector instanceof NodeList)
|
|
18
23
|
return selector
|
|
19
24
|
|
|
20
|
-
return document.querySelectorAll<LazyImage>('.smooth-loader')
|
|
25
|
+
return document.querySelectorAll<LazyImage>('.smooth-loader[data-src]')
|
|
21
26
|
}
|
package/src/types.ts
CHANGED