smooth-loader 1.1.3 → 1.2.0
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/.dockerignore +3 -0
- package/.github/workflows/npm-publish.yml +19 -0
- package/.prettierrc +8 -0
- package/CHANGELOG.md +25 -46
- package/Dockerfile +15 -0
- package/LICENSE +1 -1
- package/README.md +35 -1
- package/docker-compose.yml +13 -0
- package/lib/js/imageLoader.d.ts +2 -2
- package/lib/js/imageLoader.d.ts.map +1 -1
- package/lib/js/imageLoader.js +18 -15
- package/lib/js/imageLoader.js.map +1 -1
- package/lib/js/index.d.ts.map +1 -1
- package/lib/js/index.js +2 -2
- package/lib/js/index.js.map +1 -1
- package/lib/js/{handler.d.ts → observerCreator.d.ts} +3 -3
- package/lib/js/observerCreator.d.ts.map +1 -0
- package/lib/js/observerCreator.js +41 -0
- package/lib/js/observerCreator.js.map +1 -0
- package/lib/js/types.d.ts +2 -2
- package/lib/js/types.d.ts.map +1 -1
- package/package.json +23 -24
- package/src/imageLoader.ts +23 -18
- package/src/index.ts +26 -24
- package/src/observerCreator.ts +45 -0
- package/src/types.ts +2 -2
- package/lib/js/LazyLoader.d.ts +0 -27
- package/lib/js/LazyLoader.d.ts.map +0 -1
- package/lib/js/LazyLoader.js +0 -60
- package/lib/js/LazyLoader.js.map +0 -1
- package/lib/js/handler.d.ts.map +0 -1
- package/lib/js/handler.js +0 -30
- package/lib/js/handler.js.map +0 -1
- package/lib/js/loader.d.ts +0 -8
- package/lib/js/loader.d.ts.map +0 -1
- package/lib/js/loader.js +0 -55
- package/lib/js/loader.js.map +0 -1
- package/src/handler.ts +0 -32
package/.dockerignore
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
on:
|
|
2
|
+
push:
|
|
3
|
+
branches: master
|
|
4
|
+
|
|
5
|
+
jobs:
|
|
6
|
+
publish:
|
|
7
|
+
runs-on: ubuntu-latest
|
|
8
|
+
steps:
|
|
9
|
+
- uses: actions/checkout@v2
|
|
10
|
+
|
|
11
|
+
- name: Setup Node.js
|
|
12
|
+
uses: actions/setup-node@v2
|
|
13
|
+
with:
|
|
14
|
+
node-version: '20'
|
|
15
|
+
registry-url: 'https://registry.npmjs.org/'
|
|
16
|
+
- run: npm install
|
|
17
|
+
- run: npm publish
|
|
18
|
+
env:
|
|
19
|
+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
|
package/.prettierrc
ADDED
package/CHANGELOG.md
CHANGED
|
@@ -1,65 +1,44 @@
|
|
|
1
1
|
# Release Notes
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
## v1.2.0 (2025-06-29)
|
|
4
|
+
- Added Docker support to the project
|
|
4
5
|
|
|
5
|
-
## v1.1.
|
|
6
|
-
|
|
7
|
-
-
|
|
6
|
+
## v1.1.4 (2024-04-08)
|
|
7
|
+
- Formatted the `CHANGELOG.md` file
|
|
8
|
+
- Refactored the code to make it more readable
|
|
9
|
+
- Improved error handling
|
|
8
10
|
|
|
9
|
-
|
|
11
|
+
## v1.1.3 (2023-11-19)
|
|
12
|
+
- Changed the `README.MD` file
|
|
10
13
|
|
|
11
14
|
## v1.1.2 (2022-12-04)
|
|
12
|
-
|
|
13
|
-
- Removed line from docs;
|
|
14
|
-
|
|
15
|
-
----
|
|
15
|
+
- Removed line from docs
|
|
16
16
|
|
|
17
17
|
## v1.1.1 (2022-12-04)
|
|
18
|
-
|
|
19
|
-
-
|
|
20
|
-
-
|
|
21
|
-
- Improved documentation (README.md);
|
|
22
|
-
|
|
23
|
-
----
|
|
18
|
+
- Code refactoring
|
|
19
|
+
- Added ability to provide array of images instead of only NodeListOf
|
|
20
|
+
- Improved documentation in `README.md`
|
|
24
21
|
|
|
25
22
|
## v1.1.0 (2022-12-01)
|
|
26
|
-
|
|
27
|
-
- Moved from beta version;
|
|
28
|
-
|
|
29
|
-
----
|
|
23
|
+
- Moved from beta version
|
|
30
24
|
|
|
31
25
|
## v1.1.0-beta.2 (2022-11-30)
|
|
32
|
-
|
|
33
|
-
-
|
|
34
|
-
- Manual testing;
|
|
35
|
-
|
|
36
|
-
----
|
|
26
|
+
- Code refactoring
|
|
27
|
+
- Manual testing
|
|
37
28
|
|
|
38
29
|
## v1.1.0-beta.1 (2022-11-29)
|
|
39
|
-
|
|
40
|
-
-
|
|
41
|
-
- Added more info to docs;
|
|
42
|
-
|
|
43
|
-
----
|
|
30
|
+
- Removed blur from small placeholder images if they are set with `src` attribute
|
|
31
|
+
- Added more info to docs
|
|
44
32
|
|
|
45
33
|
## v1.1.0-beta.0 (2022-11-29)
|
|
46
|
-
|
|
47
|
-
- Added
|
|
48
|
-
- Added ability to show small images as placeholder, so you don't look at empty screen but blurred mini version of the image;
|
|
49
|
-
|
|
50
|
-
----
|
|
34
|
+
- Added missing information to the README.md file
|
|
35
|
+
- Added ability to show small images as placeholder, so you don't look at empty screen but blurred mini version of the image
|
|
51
36
|
|
|
52
37
|
## v1.0.7 (2022-06-22)
|
|
38
|
+
- Fixed appearing transition with background images. Now it works
|
|
39
|
+
- Improved documentation `README.md` file
|
|
40
|
+
- Added **configurations** for smooth loading images
|
|
53
41
|
|
|
54
|
-
|
|
55
|
-
-
|
|
56
|
-
- Added
|
|
57
|
-
|
|
58
|
-
----
|
|
59
|
-
|
|
60
|
-
## v1.0.6 (2022-01-2)
|
|
61
|
-
|
|
62
|
-
- Changed default images selector to be more specific.
|
|
63
|
-
- Added `CHANGELOG.md` file.
|
|
64
|
-
|
|
65
|
-
----
|
|
42
|
+
## v1.0.6 (2022-01-02)
|
|
43
|
+
- Changed default images selector to be more specific
|
|
44
|
+
- Added `CHANGELOG.md` file
|
package/Dockerfile
ADDED
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -4,4 +4,38 @@ Smooth loader allows you smoothly lazy load images and background images. Lazy L
|
|
|
4
4
|
|
|
5
5
|
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.
|
|
6
6
|
|
|
7
|
-
## Follow the [official documentation](https://smooth-loader.github.io) for all the details
|
|
7
|
+
## Follow the [official documentation](https://smooth-loader.github.io) for all the details
|
|
8
|
+
|
|
9
|
+
## Development
|
|
10
|
+
### Without Docker
|
|
11
|
+
You'll need to have [Node JS and NPM](https://nodejs.org/en/download) installed on your machine to run it locally.
|
|
12
|
+
|
|
13
|
+
#### Install Dependencies
|
|
14
|
+
```bash
|
|
15
|
+
npm i
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
#### Watch File Changes
|
|
19
|
+
This command will watch for your changes and compile TypeScript to JavaScript files into `lib/js`
|
|
20
|
+
```bash
|
|
21
|
+
npm run watch
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
### With [Docker](https://www.docker.com/)
|
|
25
|
+
#### Build the Image
|
|
26
|
+
To build an image, navigate to the root of the project that contains `Dockerfile` and run this command:
|
|
27
|
+
```bash
|
|
28
|
+
docker compose build
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
Now, you'll have an image `smooth-loader` that you can use to create a container.
|
|
32
|
+
|
|
33
|
+
#### Create and Enter the Container
|
|
34
|
+
To create and enter inside the container, run this command:
|
|
35
|
+
```bash
|
|
36
|
+
docker compose run --rm app
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
You'll be able to run NPM commands inside of the container and commands:
|
|
40
|
+
- `npm run watch` to make TypeScript watch for your changes and auto-compile everything to plain JavaScript
|
|
41
|
+
- `npm run build` compile all of your TypeScript files to JavaScript
|
package/lib/js/imageLoader.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ImageElement } from './types';
|
|
2
2
|
/**
|
|
3
3
|
* If image has tag IMG then set the src attribute to img url,
|
|
4
4
|
* otherwise set the background of the element to given image url
|
|
5
5
|
*/
|
|
6
|
-
export declare function loadImage(
|
|
6
|
+
export declare function loadImage(img: ImageElement): void;
|
|
7
7
|
//# sourceMappingURL=imageLoader.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"imageLoader.d.ts","sourceRoot":"","sources":["../../src/imageLoader.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"imageLoader.d.ts","sourceRoot":"","sources":["../../src/imageLoader.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAA;AAEtC;;;GAGG;AACH,wBAAgB,SAAS,CAAC,GAAG,EAAE,YAAY,GAAG,IAAI,CAoBjD"}
|
package/lib/js/imageLoader.js
CHANGED
|
@@ -1,31 +1,34 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.loadImage =
|
|
3
|
+
exports.loadImage = loadImage;
|
|
4
4
|
/**
|
|
5
5
|
* If image has tag IMG then set the src attribute to img url,
|
|
6
6
|
* otherwise set the background of the element to given image url
|
|
7
7
|
*/
|
|
8
|
-
function loadImage(
|
|
9
|
-
const imageUrl = getImageUrl(
|
|
8
|
+
function loadImage(img) {
|
|
9
|
+
const imageUrl = getImageUrl(img);
|
|
10
10
|
if (!imageUrl) {
|
|
11
|
+
console.warn('SmoothLoader: "data-src" attribute is missing or empty');
|
|
11
12
|
return;
|
|
12
13
|
}
|
|
13
|
-
if (!
|
|
14
|
-
|
|
14
|
+
if (!img.hasAttribute('src')) {
|
|
15
|
+
img.style.opacity = '0';
|
|
15
16
|
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
lazyImage.style.opacity = '1';
|
|
20
|
-
});
|
|
21
|
-
ghostImage.src = imageUrl;
|
|
22
|
-
if (isImageElement(lazyImage)) {
|
|
23
|
-
lazyImage.setAttribute('src', imageUrl);
|
|
17
|
+
preloadAndFadeInImage(img, imageUrl);
|
|
18
|
+
if (isImageElement(img)) {
|
|
19
|
+
img.setAttribute('src', imageUrl);
|
|
24
20
|
return;
|
|
25
21
|
}
|
|
26
|
-
|
|
22
|
+
img.style.backgroundImage = `url(${imageUrl})`;
|
|
23
|
+
}
|
|
24
|
+
function preloadAndFadeInImage(img, imageUrl) {
|
|
25
|
+
const preloadedImage = new Image();
|
|
26
|
+
preloadedImage.addEventListener('load', () => {
|
|
27
|
+
img.style.transition = 'opacity 777ms';
|
|
28
|
+
img.style.opacity = '1';
|
|
29
|
+
});
|
|
30
|
+
preloadedImage.src = imageUrl;
|
|
27
31
|
}
|
|
28
|
-
exports.loadImage = loadImage;
|
|
29
32
|
const isImageElement = (img) => img.tagName === 'IMG';
|
|
30
33
|
const getImageUrl = (img) => img.getAttribute('data-src');
|
|
31
34
|
//# sourceMappingURL=imageLoader.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"imageLoader.js","sourceRoot":"","sources":["../../src/imageLoader.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"imageLoader.js","sourceRoot":"","sources":["../../src/imageLoader.ts"],"names":[],"mappings":";;AAMA,8BAoBC;AAxBD;;;GAGG;AACH,SAAgB,SAAS,CAAC,GAAiB;IACvC,MAAM,QAAQ,GAAG,WAAW,CAAC,GAAG,CAAC,CAAA;IAEjC,IAAI,CAAC,QAAQ,EAAE,CAAC;QACZ,OAAO,CAAC,IAAI,CAAC,wDAAwD,CAAC,CAAA;QACtE,OAAM;IACV,CAAC;IAED,IAAI,CAAC,GAAG,CAAC,YAAY,CAAC,KAAK,CAAC,EAAE,CAAC;QAC3B,GAAG,CAAC,KAAK,CAAC,OAAO,GAAG,GAAG,CAAA;IAC3B,CAAC;IAED,qBAAqB,CAAC,GAAG,EAAE,QAAQ,CAAC,CAAA;IAEpC,IAAI,cAAc,CAAC,GAAG,CAAC,EAAE,CAAC;QACtB,GAAG,CAAC,YAAY,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAA;QACjC,OAAM;IACV,CAAC;IAED,GAAG,CAAC,KAAK,CAAC,eAAe,GAAG,OAAO,QAAQ,GAAG,CAAA;AAClD,CAAC;AAED,SAAS,qBAAqB,CAAC,GAAiB,EAAE,QAAgB;IAC9D,MAAM,cAAc,GAAG,IAAI,KAAK,EAAE,CAAA;IAElC,cAAc,CAAC,gBAAgB,CAAC,MAAM,EAAE,GAAG,EAAE;QACzC,GAAG,CAAC,KAAK,CAAC,UAAU,GAAG,eAAe,CAAA;QACtC,GAAG,CAAC,KAAK,CAAC,OAAO,GAAG,GAAG,CAAA;IAC3B,CAAC,CAAC,CAAA;IAEF,cAAc,CAAC,GAAG,GAAG,QAAQ,CAAA;AACjC,CAAC;AAED,MAAM,cAAc,GAAG,CAAC,GAAiB,EAAE,EAAE,CAAC,GAAG,CAAC,OAAO,KAAK,KAAK,CAAA;AACnE,MAAM,WAAW,GAAG,CAAC,GAAiB,EAAE,EAAE,CAAC,GAAG,CAAC,YAAY,CAAC,UAAU,CAAC,CAAA"}
|
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,EAAgB,UAAU,EAAE,MAAM,SAAS,CAAA;yBAE1C,WAAW,UAAU,EAAE,SAAS,MAAM,KAAG,IAAI;AAA7D,wBAaC"}
|
package/lib/js/index.js
CHANGED
|
@@ -3,7 +3,7 @@ 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 observerCreator_1 = __importDefault(require("./observerCreator"));
|
|
7
7
|
exports.default = (selector, config) => {
|
|
8
8
|
const images = getImagesElements(selector);
|
|
9
9
|
if (!images) {
|
|
@@ -13,7 +13,7 @@ exports.default = (selector, config) => {
|
|
|
13
13
|
root: null,
|
|
14
14
|
threshold: 0,
|
|
15
15
|
};
|
|
16
|
-
return (0,
|
|
16
|
+
return (0, observerCreator_1.default)(images, configurations);
|
|
17
17
|
};
|
|
18
18
|
function getImagesElements(selector) {
|
|
19
19
|
if (!selector) {
|
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,wEAA+C;AAG/C,kBAAe,CAAC,QAAqB,EAAE,MAAe,EAAQ,EAAE;IAC/D,MAAM,MAAM,GAAG,iBAAiB,CAAC,QAAQ,CAAC,CAAA;IAE1C,IAAI,CAAC,MAAM,EAAE,CAAC;QACb,OAAM;IACP,CAAC;IAED,MAAM,cAAc,GAAG,MAAM,IAAI;QAChC,IAAI,EAAE,IAAI;QACV,SAAS,EAAE,CAAC;KACZ,CAAA;IAED,OAAO,IAAA,yBAAe,EAAC,MAAM,EAAE,cAAc,CAAC,CAAA;AAC/C,CAAC,CAAA;AAED,SAAS,iBAAiB,CAAC,QAAqB;IAC/C,IAAI,CAAC,QAAQ,EAAE,CAAC;QACf,MAAM,QAAQ,GAAG,QAAQ,CAAC,gBAAgB,CACzC,0BAA0B,CAC1B,CAAA;QACD,OAAO,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAA;IAC5B,CAAC;IAED,IAAI,OAAO,QAAQ,KAAK,QAAQ,EAAE,CAAC;QAClC,MAAM,QAAQ,GAAG,QAAQ,CAAC,gBAAgB,CAAe,QAAQ,CAAC,CAAA;QAClE,OAAO,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAA;IAC5B,CAAC;IAED,IAAI,QAAQ,YAAY,QAAQ,EAAE,CAAC;QAClC,OAAO,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAA;IAC5B,CAAC;IAED,OAAO,QAAQ,CAAA;AAChB,CAAC"}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { Config,
|
|
1
|
+
import { Config, ImageElement } from './types';
|
|
2
2
|
/**
|
|
3
3
|
* Creates image object, gets attributes from placeholder,
|
|
4
4
|
* sets them on image object, adds classes to image and
|
|
5
5
|
* when image is loaded, appends it to a placeholder
|
|
6
6
|
*/
|
|
7
|
-
export default function (images:
|
|
8
|
-
//# sourceMappingURL=
|
|
7
|
+
export default function (images: ImageElement[], config: Config): void;
|
|
8
|
+
//# sourceMappingURL=observerCreator.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"observerCreator.d.ts","sourceRoot":"","sources":["../../src/observerCreator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,SAAS,CAAA;AAG9C;;;;GAIG;AACH,MAAM,CAAC,OAAO,WAAW,MAAM,EAAE,YAAY,EAAE,EAAE,MAAM,EAAE,MAAM,GAAG,IAAI,CASrE"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.default = default_1;
|
|
4
|
+
const imageLoader_1 = require("./imageLoader");
|
|
5
|
+
/**
|
|
6
|
+
* Creates image object, gets attributes from placeholder,
|
|
7
|
+
* sets them on image object, adds classes to image and
|
|
8
|
+
* when image is loaded, appends it to a placeholder
|
|
9
|
+
*/
|
|
10
|
+
function default_1(images, config) {
|
|
11
|
+
for (const img of images) {
|
|
12
|
+
if (browserSupportsIntersectionObserver()) {
|
|
13
|
+
applyIntersectionObserverTo(img, config);
|
|
14
|
+
continue;
|
|
15
|
+
}
|
|
16
|
+
(0, imageLoader_1.loadImage)(img);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
function browserSupportsIntersectionObserver() {
|
|
20
|
+
return 'IntersectionObserver' in window;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Creates instance of IntersectionObserver and loads image in DOM
|
|
24
|
+
* as soon as image will be visible on the screen
|
|
25
|
+
*/
|
|
26
|
+
function applyIntersectionObserverTo(img, config) {
|
|
27
|
+
// This callback will be called every time the image is visible on the screen
|
|
28
|
+
const handleObserver = (entries, observer) => {
|
|
29
|
+
for (const entry of entries) {
|
|
30
|
+
// If image is not visible on the screen, do nothing
|
|
31
|
+
if (!entry.isIntersecting) {
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
(0, imageLoader_1.loadImage)(img);
|
|
35
|
+
// Stop observing image after it's loaded
|
|
36
|
+
observer.unobserve(img);
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
new IntersectionObserver(handleObserver, config).observe(img);
|
|
40
|
+
}
|
|
41
|
+
//# sourceMappingURL=observerCreator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"observerCreator.js","sourceRoot":"","sources":["../../src/observerCreator.ts"],"names":[],"mappings":";;AAQA,4BASC;AAhBD,+CAAyC;AAEzC;;;;GAIG;AACH,mBAAyB,MAAsB,EAAE,MAAc;IAC3D,KAAK,MAAM,GAAG,IAAI,MAAM,EAAE,CAAC;QACvB,IAAI,mCAAmC,EAAE,EAAE,CAAC;YACxC,2BAA2B,CAAC,GAAG,EAAE,MAAM,CAAC,CAAA;YACxC,SAAQ;QACZ,CAAC;QAED,IAAA,uBAAS,EAAC,GAAG,CAAC,CAAA;IAClB,CAAC;AACL,CAAC;AAED,SAAS,mCAAmC;IACxC,OAAO,sBAAsB,IAAI,MAAM,CAAA;AAC3C,CAAC;AAED;;;GAGG;AACH,SAAS,2BAA2B,CAAC,GAAiB,EAAE,MAAc;IAClE,6EAA6E;IAC7E,MAAM,cAAc,GAAG,CAAC,OAAoC,EAAE,QAA8B,EAAE,EAAE;QAC5F,KAAK,MAAM,KAAK,IAAI,OAAO,EAAE,CAAC;YAC1B,oDAAoD;YACpD,IAAI,CAAC,KAAK,CAAC,cAAc,EAAE,CAAC;gBACxB,SAAQ;YACZ,CAAC;YAED,IAAA,uBAAS,EAAC,GAAG,CAAC,CAAA;YAEd,yCAAyC;YACzC,QAAQ,CAAC,SAAS,CAAC,GAAG,CAAC,CAAA;QAC3B,CAAC;IACL,CAAC,CAAA;IAED,IAAI,oBAAoB,CAAC,cAAc,EAAE,MAAM,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,CAAA;AACjE,CAAC"}
|
package/lib/js/types.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
export
|
|
2
|
-
export
|
|
1
|
+
export type ImageElement = HTMLDivElement | HTMLImageElement;
|
|
2
|
+
export type InputTypes = NodeListOf<ImageElement> | ImageElement[] | string;
|
|
3
3
|
export interface Config extends IntersectionObserverInit {
|
|
4
4
|
}
|
|
5
5
|
//# sourceMappingURL=types.d.ts.map
|
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,
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,YAAY,GAAG,cAAc,GAAG,gBAAgB,CAAA;AAC5D,MAAM,MAAM,UAAU,GAAG,UAAU,CAAC,YAAY,CAAC,GAAG,YAAY,EAAE,GAAG,MAAM,CAAA;AAE3E,MAAM,WAAW,MAAO,SAAQ,wBAAwB;CAAI"}
|
package/package.json
CHANGED
|
@@ -1,25 +1,24 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
"
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
"
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
"
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
}
|
|
2
|
+
"name": "smooth-loader",
|
|
3
|
+
"version": "1.2.0",
|
|
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
|
+
"main": "lib/js/index.js",
|
|
6
|
+
"types": "lib/js",
|
|
7
|
+
"author": "Serhii Cho",
|
|
8
|
+
"license": "MIT",
|
|
9
|
+
"homepage": "https://smooth-loader.github.io",
|
|
10
|
+
"devDependencies": {
|
|
11
|
+
"typescript": "^5.8.3"
|
|
12
|
+
},
|
|
13
|
+
"scripts": {
|
|
14
|
+
"build": "tsc -p .",
|
|
15
|
+
"watch": "tsc --watch"
|
|
16
|
+
},
|
|
17
|
+
"keywords": [
|
|
18
|
+
"lazy loader",
|
|
19
|
+
"lazy",
|
|
20
|
+
"image loader",
|
|
21
|
+
"lazy loading",
|
|
22
|
+
"background image"
|
|
23
|
+
]
|
|
24
|
+
}
|
package/src/imageLoader.ts
CHANGED
|
@@ -1,36 +1,41 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ImageElement } from './types'
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* If image has tag IMG then set the src attribute to img url,
|
|
5
5
|
* otherwise set the background of the element to given image url
|
|
6
6
|
*/
|
|
7
|
-
export function loadImage(
|
|
8
|
-
const imageUrl = getImageUrl(
|
|
7
|
+
export function loadImage(img: ImageElement): void {
|
|
8
|
+
const imageUrl = getImageUrl(img)
|
|
9
9
|
|
|
10
10
|
if (!imageUrl) {
|
|
11
|
+
console.warn('SmoothLoader: "data-src" attribute is missing or empty')
|
|
11
12
|
return
|
|
12
13
|
}
|
|
13
14
|
|
|
14
|
-
if (!
|
|
15
|
-
|
|
15
|
+
if (!img.hasAttribute('src')) {
|
|
16
|
+
img.style.opacity = '0'
|
|
16
17
|
}
|
|
17
18
|
|
|
18
|
-
|
|
19
|
+
preloadAndFadeInImage(img, imageUrl)
|
|
19
20
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
lazyImage.style.opacity = '1'
|
|
23
|
-
})
|
|
24
|
-
|
|
25
|
-
ghostImage.src = imageUrl
|
|
26
|
-
|
|
27
|
-
if (isImageElement(lazyImage)) {
|
|
28
|
-
lazyImage.setAttribute('src', imageUrl)
|
|
21
|
+
if (isImageElement(img)) {
|
|
22
|
+
img.setAttribute('src', imageUrl)
|
|
29
23
|
return
|
|
30
24
|
}
|
|
31
25
|
|
|
32
|
-
|
|
26
|
+
img.style.backgroundImage = `url(${imageUrl})`
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function preloadAndFadeInImage(img: ImageElement, imageUrl: string): void {
|
|
30
|
+
const preloadedImage = new Image()
|
|
31
|
+
|
|
32
|
+
preloadedImage.addEventListener('load', () => {
|
|
33
|
+
img.style.transition = 'opacity 777ms'
|
|
34
|
+
img.style.opacity = '1'
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
preloadedImage.src = imageUrl
|
|
33
38
|
}
|
|
34
39
|
|
|
35
|
-
const isImageElement = (img:
|
|
36
|
-
const getImageUrl = (img:
|
|
40
|
+
const isImageElement = (img: ImageElement) => img.tagName === 'IMG'
|
|
41
|
+
const getImageUrl = (img: ImageElement) => img.getAttribute('data-src')
|
package/src/index.ts
CHANGED
|
@@ -1,35 +1,37 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { Config,
|
|
1
|
+
import observerCreator from './observerCreator'
|
|
2
|
+
import { Config, ImageElement, InputTypes } from './types'
|
|
3
3
|
|
|
4
4
|
export default (selector?: InputTypes, config?: Config): void => {
|
|
5
|
-
|
|
5
|
+
const images = getImagesElements(selector)
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
if (!images) {
|
|
8
|
+
return
|
|
9
|
+
}
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
11
|
+
const configurations = config || {
|
|
12
|
+
root: null,
|
|
13
|
+
threshold: 0,
|
|
14
|
+
}
|
|
15
15
|
|
|
16
|
-
|
|
16
|
+
return observerCreator(images, configurations)
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
-
function getImagesElements(selector?: InputTypes):
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
19
|
+
function getImagesElements(selector?: InputTypes): ImageElement[] {
|
|
20
|
+
if (!selector) {
|
|
21
|
+
const elements = document.querySelectorAll<ImageElement>(
|
|
22
|
+
'.smooth-loader[data-src]',
|
|
23
|
+
)
|
|
24
|
+
return Array.from(elements)
|
|
25
|
+
}
|
|
24
26
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
27
|
+
if (typeof selector === 'string') {
|
|
28
|
+
const elements = document.querySelectorAll<ImageElement>(selector)
|
|
29
|
+
return Array.from(elements)
|
|
30
|
+
}
|
|
29
31
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
32
|
+
if (selector instanceof NodeList) {
|
|
33
|
+
return Array.from(selector)
|
|
34
|
+
}
|
|
33
35
|
|
|
34
|
-
|
|
36
|
+
return selector
|
|
35
37
|
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { Config, ImageElement } 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: ImageElement[], config: Config): void {
|
|
10
|
+
for (const img of images) {
|
|
11
|
+
if (browserSupportsIntersectionObserver()) {
|
|
12
|
+
applyIntersectionObserverTo(img, config)
|
|
13
|
+
continue
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
loadImage(img)
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function browserSupportsIntersectionObserver(): boolean {
|
|
21
|
+
return 'IntersectionObserver' in window
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Creates instance of IntersectionObserver and loads image in DOM
|
|
26
|
+
* as soon as image will be visible on the screen
|
|
27
|
+
*/
|
|
28
|
+
function applyIntersectionObserverTo(img: ImageElement, config: Config): void {
|
|
29
|
+
// This callback will be called every time the image is visible on the screen
|
|
30
|
+
const handleObserver = (entries: IntersectionObserverEntry[], observer: IntersectionObserver) => {
|
|
31
|
+
for (const entry of entries) {
|
|
32
|
+
// If image is not visible on the screen, do nothing
|
|
33
|
+
if (!entry.isIntersecting) {
|
|
34
|
+
continue
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
loadImage(img)
|
|
38
|
+
|
|
39
|
+
// Stop observing image after it's loaded
|
|
40
|
+
observer.unobserve(img)
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
new IntersectionObserver(handleObserver, config).observe(img)
|
|
45
|
+
}
|
package/src/types.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export type
|
|
2
|
-
export type InputTypes = NodeListOf<
|
|
1
|
+
export type ImageElement = HTMLDivElement | HTMLImageElement
|
|
2
|
+
export type InputTypes = NodeListOf<ImageElement> | ImageElement[] | string
|
|
3
3
|
|
|
4
4
|
export interface Config extends IntersectionObserverInit { }
|
package/lib/js/LazyLoader.d.ts
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import { Config, LazyImage } from './types';
|
|
2
|
-
/**
|
|
3
|
-
* Class that handles lazy loading images with
|
|
4
|
-
* Intersection Observer
|
|
5
|
-
*/
|
|
6
|
-
export default class {
|
|
7
|
-
private readonly images;
|
|
8
|
-
private readonly config;
|
|
9
|
-
constructor(images: NodeListOf<LazyImage>, config: Config);
|
|
10
|
-
/**
|
|
11
|
-
* If image has tag IMG then set the src attribute to img url,
|
|
12
|
-
* otherwise set the background of the element to given image url
|
|
13
|
-
*/
|
|
14
|
-
private loadImage;
|
|
15
|
-
/**
|
|
16
|
-
* Create observer object that will trigger loading image function
|
|
17
|
-
* when it's gonna be visible on the screen
|
|
18
|
-
*/
|
|
19
|
-
private createObserver;
|
|
20
|
-
/**
|
|
21
|
-
* Creates image object, gets attributes from placeholder,
|
|
22
|
-
* sets them on image object, adds classes to image and
|
|
23
|
-
* when image is loaded, appends it to a placeholder
|
|
24
|
-
*/
|
|
25
|
-
execute(): void;
|
|
26
|
-
}
|
|
27
|
-
//# sourceMappingURL=LazyLoader.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"LazyLoader.d.ts","sourceRoot":"","sources":["../../src/LazyLoader.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,SAAS,CAAA;AAE3C;;;GAGG;AACH,MAAM,CAAC,OAAO;IAEN,OAAO,CAAC,QAAQ,CAAC,MAAM;IACvB,OAAO,CAAC,QAAQ,CAAC,MAAM;gBADN,MAAM,EAAE,UAAU,CAAC,SAAS,CAAC,EAC7B,MAAM,EAAE,MAAM;IAInC;;;OAGG;IACH,OAAO,CAAC,SAAS;IAyBjB;;;OAGG;IACH,OAAO,CAAC,cAAc;IAatB;;;;OAIG;IACI,OAAO,IAAI,IAAI;CAKzB"}
|
package/lib/js/LazyLoader.js
DELETED
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
/**
|
|
4
|
-
* Class that handles lazy loading images with
|
|
5
|
-
* Intersection Observer
|
|
6
|
-
*/
|
|
7
|
-
class default_1 {
|
|
8
|
-
constructor(images, config) {
|
|
9
|
-
this.images = images;
|
|
10
|
-
this.config = config;
|
|
11
|
-
}
|
|
12
|
-
/**
|
|
13
|
-
* If image has tag IMG then set the src attribute to img url,
|
|
14
|
-
* otherwise set the background of the element to given image url
|
|
15
|
-
*/
|
|
16
|
-
loadImage(img) {
|
|
17
|
-
const imageUrl = img.getAttribute('data-src');
|
|
18
|
-
if (!imageUrl)
|
|
19
|
-
return;
|
|
20
|
-
if (!img.hasAttribute('src')) {
|
|
21
|
-
img.style.opacity = '0';
|
|
22
|
-
}
|
|
23
|
-
const ghostImage = new Image();
|
|
24
|
-
ghostImage.addEventListener('load', () => {
|
|
25
|
-
img.style.transition = 'opacity 777ms';
|
|
26
|
-
img.style.opacity = '1';
|
|
27
|
-
});
|
|
28
|
-
ghostImage.src = imageUrl;
|
|
29
|
-
img.tagName === 'IMG'
|
|
30
|
-
? img.setAttribute('src', imageUrl)
|
|
31
|
-
: img.style.backgroundImage = `url(${imageUrl})`;
|
|
32
|
-
}
|
|
33
|
-
/**
|
|
34
|
-
* Create observer object that will trigger loading image function
|
|
35
|
-
* when it's gonna be visible on the screen
|
|
36
|
-
*/
|
|
37
|
-
createObserver(img) {
|
|
38
|
-
const observer = new IntersectionObserver((entries, observer) => {
|
|
39
|
-
entries.forEach(entry => {
|
|
40
|
-
if (entry.isIntersecting) {
|
|
41
|
-
this.loadImage(img);
|
|
42
|
-
observer.unobserve(img);
|
|
43
|
-
}
|
|
44
|
-
});
|
|
45
|
-
}, this.config);
|
|
46
|
-
observer.observe(img);
|
|
47
|
-
}
|
|
48
|
-
/**
|
|
49
|
-
* Creates image object, gets attributes from placeholder,
|
|
50
|
-
* sets them on image object, adds classes to image and
|
|
51
|
-
* when image is loaded, appends it to a placeholder
|
|
52
|
-
*/
|
|
53
|
-
execute() {
|
|
54
|
-
this.images.forEach(img => {
|
|
55
|
-
!window['IntersectionObserver'] ? this.loadImage(img) : this.createObserver(img);
|
|
56
|
-
});
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
exports.default = default_1;
|
|
60
|
-
//# sourceMappingURL=LazyLoader.js.map
|
package/lib/js/LazyLoader.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"LazyLoader.js","sourceRoot":"","sources":["../../src/LazyLoader.ts"],"names":[],"mappings":";;AAEA;;;GAGG;AACH;IACI,YACqB,MAA6B,EAC7B,MAAc;QADd,WAAM,GAAN,MAAM,CAAuB;QAC7B,WAAM,GAAN,MAAM,CAAQ;IAEnC,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,IAAI,CAAC,GAAG,CAAC,YAAY,CAAC,KAAK,CAAC,EAAE;YAC1B,GAAG,CAAC,KAAK,CAAC,OAAO,GAAG,GAAG,CAAA;SAC1B;QAED,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,MAAM,CAAC,CAAA;QAEf,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/handler.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
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"}
|
package/lib/js/handler.js
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
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
|
package/lib/js/handler.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
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"}
|
package/lib/js/loader.d.ts
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
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
|
package/lib/js/loader.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
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
DELETED
|
@@ -1,55 +0,0 @@
|
|
|
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
|
package/lib/js/loader.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
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/src/handler.ts
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
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
|
-
}
|