smooth-loader 1.0.1 → 1.0.5
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/LICENSE +21 -0
- package/README.md +33 -8
- package/lib/css/index.css +1 -1
- package/lib/js/LazyLoader.d.ts +2 -1
- package/lib/js/LazyLoader.d.ts.map +1 -1
- package/lib/js/LazyLoader.js +8 -6
- package/lib/js/LazyLoader.js.map +1 -1
- package/lib/js/index.d.ts +2 -1
- package/lib/js/index.d.ts.map +1 -1
- package/lib/js/index.js +10 -4
- package/lib/js/index.js.map +1 -1
- package/lib/js/types.d.ts +2 -0
- package/lib/js/types.d.ts.map +1 -0
- package/lib/js/types.js +3 -0
- package/lib/js/types.js.map +1 -0
- package/package.json +3 -2
- package/src/LazyLoader.ts +17 -11
- package/src/index.ts +14 -4
- package/src/types.ts +1 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021 Serhii Cho
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,12 +1,17 @@
|
|
|
1
1
|
# Description
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
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
|
+
|
|
5
|
+
- [NPM](https://www.npmjs.com/package/smooth-loader)
|
|
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)
|
|
4
8
|
|
|
5
9
|
# Features
|
|
6
10
|
|
|
7
|
-
-
|
|
8
|
-
-
|
|
9
|
-
- Loads images with
|
|
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
|
|
10
15
|
|
|
11
16
|
# Installation
|
|
12
17
|
|
|
@@ -18,20 +23,34 @@ npm i smooth-loader
|
|
|
18
23
|
yarn add smooth-loader
|
|
19
24
|
```
|
|
20
25
|
|
|
21
|
-
Import `smooth-loader` and the core CSS. This assumes you're using a module bundler like webpack.
|
|
22
|
-
|
|
23
26
|
```js
|
|
24
27
|
import smoothLoader from 'smooth-loader'
|
|
25
|
-
|
|
28
|
+
|
|
29
|
+
smoothLoader()
|
|
26
30
|
```
|
|
27
31
|
|
|
28
32
|
# Usage
|
|
29
33
|
|
|
34
|
+
To make image or element Lazy loaded, replace `src` attribute with `data-src`
|
|
35
|
+
on image element, and add `data-src` attribute to elements that have background
|
|
36
|
+
image that you want to lazy load.
|
|
37
|
+
|
|
38
|
+
- Add **smooth-loader** css class to each image or element that you want or specify your own selector for images instead of **smooth-loader**.
|
|
39
|
+
- Call `smoothLoader()` function when you want to lazy load images.
|
|
40
|
+
|
|
30
41
|
```js
|
|
42
|
+
// with .smooth-loader selector
|
|
31
43
|
smoothLoader()
|
|
44
|
+
|
|
45
|
+
// with custom string selector
|
|
46
|
+
smoothLoader('.lazy-image')
|
|
47
|
+
|
|
48
|
+
// with custom elements
|
|
49
|
+
const myImages = document.querySelectorAll<HTMLImageElement>('img')
|
|
50
|
+
smoothLoader(myImages)
|
|
32
51
|
```
|
|
33
52
|
|
|
34
|
-
|
|
53
|
+
> 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.
|
|
35
54
|
|
|
36
55
|
```html
|
|
37
56
|
<!-- Regular image -->
|
|
@@ -39,4 +58,10 @@ Replace `src` attribute with `data-src` on image and add `smooth-loader` css cla
|
|
|
39
58
|
|
|
40
59
|
<!-- Background image -->
|
|
41
60
|
<div data-src="./images/we.png" class="smooth-loader my-bg-image"></div>
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
# Function types
|
|
64
|
+
|
|
65
|
+
```js
|
|
66
|
+
function smoothLoader(selector?: NodeListOf<HTMLDivElement | HTMLImageElement> | string) => void
|
|
42
67
|
```
|
package/lib/css/index.css
CHANGED
package/lib/js/LazyLoader.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { LazyImage } from './types';
|
|
1
2
|
/**
|
|
2
3
|
* Class that handles lazy loading images with
|
|
3
4
|
* Intersection Observer
|
|
@@ -5,7 +6,7 @@
|
|
|
5
6
|
export default class {
|
|
6
7
|
private readonly images;
|
|
7
8
|
private observerOptions;
|
|
8
|
-
constructor(images: NodeListOf<
|
|
9
|
+
constructor(images: NodeListOf<LazyImage>);
|
|
9
10
|
/**
|
|
10
11
|
* If image has tag IMG then set the src attribute to img url,
|
|
11
12
|
* otherwise set the background of the element to given image url
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"LazyLoader.d.ts","sourceRoot":"","sources":["../../src/LazyLoader.ts"],"names":[],"mappings":"AAAA;;;GAGG;
|
|
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;IACV,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAuB;IAC9C,OAAO,CAAC,eAAe,CAGtB;gBAEkB,MAAM,EAAE,UAAU,CAAC,SAAS,CAAC;IAIhD;;;OAGG;IACH,OAAO,CAAC,SAAS;IAkBjB;;OAEG;IACH,OAAO,CAAC,cAAc;IAatB;;;;OAIG;IACI,OAAO,IAAI,IAAI;CAKzB"}
|
package/lib/js/LazyLoader.js
CHANGED
|
@@ -17,16 +17,18 @@ class default_1 {
|
|
|
17
17
|
* otherwise set the background of the element to given image url
|
|
18
18
|
*/
|
|
19
19
|
loadImage(img) {
|
|
20
|
-
const
|
|
21
|
-
|
|
20
|
+
const imageUrl = img.getAttribute('data-src');
|
|
21
|
+
img.style.opacity = '0';
|
|
22
|
+
img.style.transition = 'opacity 777ms';
|
|
23
|
+
if (!imageUrl)
|
|
22
24
|
return;
|
|
23
25
|
if (img.tagName === 'IMG') {
|
|
24
|
-
img.setAttribute('src',
|
|
25
|
-
img.addEventListener('load', () => img.
|
|
26
|
+
img.setAttribute('src', imageUrl);
|
|
27
|
+
img.addEventListener('load', () => img.style.opacity = '1');
|
|
26
28
|
}
|
|
27
29
|
else {
|
|
28
|
-
img.style.
|
|
29
|
-
setTimeout(() => img.
|
|
30
|
+
img.style.backgroundImage = `url(${img.getAttribute('data-src')})`;
|
|
31
|
+
setTimeout(() => img.style.opacity = '1', 0);
|
|
30
32
|
}
|
|
31
33
|
}
|
|
32
34
|
/**
|
package/lib/js/LazyLoader.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"LazyLoader.js","sourceRoot":"","sources":["../../src/LazyLoader.ts"],"names":[],"mappings":";;
|
|
1
|
+
{"version":3,"file":"LazyLoader.js","sourceRoot":"","sources":["../../src/LazyLoader.ts"],"names":[],"mappings":";;AAEA;;;GAGG;AACH;IAOI,YAAmB,MAA6B;QALxC,oBAAe,GAAG;YACtB,IAAI,EAAE,IAAI;YACV,SAAS,EAAE,CAAC;SACf,CAAA;QAGG,IAAI,CAAC,MAAM,GAAG,MAAM,CAAA;IACxB,CAAC;IAED;;;OAGG;IACK,SAAS,CAAC,GAAc;QAC5B,MAAM,QAAQ,GAAG,GAAG,CAAC,YAAY,CAAC,UAAU,CAAC,CAAA;QAE7C,GAAG,CAAC,KAAK,CAAC,OAAO,GAAG,GAAG,CAAA;QACvB,GAAG,CAAC,KAAK,CAAC,UAAU,GAAG,eAAe,CAAA;QAEtC,IAAI,CAAC,QAAQ;YACT,OAAM;QAEV,IAAI,GAAG,CAAC,OAAO,KAAK,KAAK,EAAE;YACvB,GAAG,CAAC,YAAY,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAA;YACjC,GAAG,CAAC,gBAAgB,CAAC,MAAM,EAAE,GAAG,EAAE,CAAC,GAAG,CAAC,KAAK,CAAC,OAAO,GAAG,GAAG,CAAC,CAAA;SAC9D;aAAM;YACH,GAAG,CAAC,KAAK,CAAC,eAAe,GAAG,OAAO,GAAG,CAAC,YAAY,CAAC,UAAU,CAAC,GAAG,CAAA;YAClE,UAAU,CAAC,GAAG,EAAE,CAAC,GAAG,CAAC,KAAK,CAAC,OAAO,GAAG,GAAG,EAAE,CAAC,CAAC,CAAA;SAC/C;IACL,CAAC;IAED;;OAEG;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;AA3DD,4BA2DC"}
|
package/lib/js/index.d.ts
CHANGED
package/lib/js/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,SAAS,CAAA;mFAEyB,IAAI;AAAhE,wBAOC"}
|
package/lib/js/index.js
CHANGED
|
@@ -4,11 +4,17 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
const LazyLoader_1 = __importDefault(require("./LazyLoader"));
|
|
7
|
-
exports.default = () => {
|
|
8
|
-
const images =
|
|
9
|
-
if (!images)
|
|
7
|
+
exports.default = (selector) => {
|
|
8
|
+
const images = getImagesElements(selector);
|
|
9
|
+
if (!images)
|
|
10
10
|
return;
|
|
11
|
-
}
|
|
12
11
|
return new LazyLoader_1.default(images).execute();
|
|
13
12
|
};
|
|
13
|
+
function getImagesElements(selector) {
|
|
14
|
+
if (typeof selector === 'string' && selector !== '')
|
|
15
|
+
return document.querySelectorAll(selector);
|
|
16
|
+
if (selector instanceof NodeList)
|
|
17
|
+
return selector;
|
|
18
|
+
return document.querySelectorAll('.smooth-loader');
|
|
19
|
+
}
|
|
14
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;
|
|
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,gBAAgB,CAAC,CAAA;AACjE,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"AAAA,oBAAY,SAAS,GAAG,cAAc,GAAG,gBAAgB,CAAA"}
|
package/lib/js/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":""}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "smooth-loader",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.5",
|
|
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,7 +9,8 @@
|
|
|
9
9
|
},
|
|
10
10
|
"scripts": {
|
|
11
11
|
"build": "tsc -p .",
|
|
12
|
-
"watch": "tsc --watch"
|
|
12
|
+
"watch": "tsc --watch",
|
|
13
|
+
"publish": "npm publish"
|
|
13
14
|
},
|
|
14
15
|
"keywords": ["lazy loader", "lazy", "image loader", "lazy loading", "background image"],
|
|
15
16
|
"author": "Serhii Cho",
|
package/src/LazyLoader.ts
CHANGED
|
@@ -1,15 +1,17 @@
|
|
|
1
|
+
import { LazyImage } from './types'
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* Class that handles lazy loading images with
|
|
3
5
|
* Intersection Observer
|
|
4
6
|
*/
|
|
5
|
-
|
|
6
|
-
private readonly images: NodeListOf<
|
|
7
|
+
export default class {
|
|
8
|
+
private readonly images: NodeListOf<LazyImage>
|
|
7
9
|
private observerOptions = {
|
|
8
10
|
root: null,
|
|
9
11
|
threshold: 0,
|
|
10
12
|
}
|
|
11
13
|
|
|
12
|
-
public constructor(images: NodeListOf<
|
|
14
|
+
public constructor(images: NodeListOf<LazyImage>) {
|
|
13
15
|
this.images = images
|
|
14
16
|
}
|
|
15
17
|
|
|
@@ -17,24 +19,28 @@
|
|
|
17
19
|
* If image has tag IMG then set the src attribute to img url,
|
|
18
20
|
* otherwise set the background of the element to given image url
|
|
19
21
|
*/
|
|
20
|
-
private loadImage(img:
|
|
21
|
-
const
|
|
22
|
+
private loadImage(img: LazyImage): void {
|
|
23
|
+
const imageUrl = img.getAttribute('data-src')
|
|
24
|
+
|
|
25
|
+
img.style.opacity = '0'
|
|
26
|
+
img.style.transition = 'opacity 777ms'
|
|
22
27
|
|
|
23
|
-
if (!
|
|
28
|
+
if (!imageUrl)
|
|
29
|
+
return
|
|
24
30
|
|
|
25
31
|
if (img.tagName === 'IMG') {
|
|
26
|
-
img.setAttribute('src',
|
|
27
|
-
img.addEventListener('load', () => img.
|
|
32
|
+
img.setAttribute('src', imageUrl)
|
|
33
|
+
img.addEventListener('load', () => img.style.opacity = '1')
|
|
28
34
|
} else {
|
|
29
|
-
img.style.
|
|
30
|
-
setTimeout(() => img.
|
|
35
|
+
img.style.backgroundImage = `url(${img.getAttribute('data-src')})`
|
|
36
|
+
setTimeout(() => img.style.opacity = '1', 0)
|
|
31
37
|
}
|
|
32
38
|
}
|
|
33
39
|
|
|
34
40
|
/**
|
|
35
41
|
* Create observer object
|
|
36
42
|
*/
|
|
37
|
-
private createObserver(img:
|
|
43
|
+
private createObserver(img: LazyImage): void {
|
|
38
44
|
const observer = new IntersectionObserver((entries, observer) => {
|
|
39
45
|
entries.forEach(entry => {
|
|
40
46
|
if (entry.isIntersecting) {
|
package/src/index.ts
CHANGED
|
@@ -1,11 +1,21 @@
|
|
|
1
1
|
import LazyLoader from "./LazyLoader"
|
|
2
|
+
import { LazyImage } from './types'
|
|
2
3
|
|
|
3
|
-
export default () => {
|
|
4
|
-
const images =
|
|
4
|
+
export default (selector?: NodeListOf<LazyImage> | string): void => {
|
|
5
|
+
const images = getImagesElements(selector)
|
|
5
6
|
|
|
6
|
-
if (!images)
|
|
7
|
+
if (!images)
|
|
7
8
|
return
|
|
8
|
-
}
|
|
9
9
|
|
|
10
10
|
return new LazyLoader(images).execute()
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function getImagesElements(selector?: NodeListOf<LazyImage> | string): NodeListOf<LazyImage> {
|
|
14
|
+
if (typeof selector === 'string' && selector !== '')
|
|
15
|
+
return document.querySelectorAll<LazyImage>(selector)
|
|
16
|
+
|
|
17
|
+
if (selector instanceof NodeList)
|
|
18
|
+
return selector
|
|
19
|
+
|
|
20
|
+
return document.querySelectorAll<LazyImage>('.smooth-loader')
|
|
11
21
|
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type LazyImage = HTMLDivElement | HTMLImageElement
|