smooth-loader 1.1.3 → 1.1.4

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 CHANGED
@@ -1,65 +1,51 @@
1
1
  # Release Notes
2
2
 
3
- ----
3
+ ## v1.1.4 (2024-04-08)
4
4
 
5
- ## v1.1.3 (2023-11-19)
5
+ - Formatted the `CHANGELOG.md` file
6
+ - Refactor the code to make it more readable
7
+ - Improve error handling
6
8
 
7
- - Changed readme.md file
9
+ ## v1.1.3 (2023-11-19)
8
10
 
9
- ----
11
+ - Changed the `README.MD` file
10
12
 
11
13
  ## v1.1.2 (2022-12-04)
12
14
 
13
- - Removed line from docs;
14
-
15
- ----
15
+ - Removed line from docs
16
16
 
17
17
  ## v1.1.1 (2022-12-04)
18
18
 
19
- - Code refactoring;
20
- - Added ability to provide array of images instead of only NodeListOf;
21
- - Improved documentation (README.md);
22
-
23
- ----
19
+ - Code refactoring
20
+ - Added ability to provide array of images instead of only NodeListOf
21
+ - Improved documentation in `README.md`
24
22
 
25
23
  ## v1.1.0 (2022-12-01)
26
24
 
27
- - Moved from beta version;
28
-
29
- ----
25
+ - Moved from beta version
30
26
 
31
27
  ## v1.1.0-beta.2 (2022-11-30)
32
28
 
33
- - Code refactoring;
34
- - Manual testing;
35
-
36
- ----
29
+ - Code refactoring
30
+ - Manual testing
37
31
 
38
32
  ## v1.1.0-beta.1 (2022-11-29)
39
33
 
40
- - Removed blur from small placeholder images if they are set with `src` attribute;
41
- - Added more info to docs;
42
-
43
- ----
34
+ - Removed blur from small placeholder images if they are set with `src` attribute
35
+ - Added more info to docs
44
36
 
45
37
  ## v1.1.0-beta.0 (2022-11-29)
46
38
 
47
- - Added missing information to the README.md file;
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
- ----
39
+ - Added missing information to the README.md file
40
+ - Added ability to show small images as placeholder, so you don't look at empty screen but blurred mini version of the image
51
41
 
52
42
  ## v1.0.7 (2022-06-22)
53
43
 
54
- - Fixed appearing transition with background images. Now it works.
55
- - Improved documentation `README.md` file.
56
- - Added **configurations** for smooth loading images.
57
-
58
- ----
59
-
60
- ## v1.0.6 (2022-01-2)
44
+ - Fixed appearing transition with background images. Now it works
45
+ - Improved documentation `README.md` file
46
+ - Added **configurations** for smooth loading images
61
47
 
62
- - Changed default images selector to be more specific.
63
- - Added `CHANGELOG.md` file.
48
+ ## v1.0.6 (2022-01-02)
64
49
 
65
- ----
50
+ - Changed default images selector to be more specific
51
+ - Added `CHANGELOG.md` file
@@ -1,7 +1,7 @@
1
- import { LazyImage } from './types';
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(lazyImage: LazyImage): void;
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,SAAS,EAAE,MAAM,SAAS,CAAA;AAEnC;;;GAGG;AACH,wBAAgB,SAAS,CAAC,SAAS,EAAE,SAAS,GAAG,IAAI,CA0BpD"}
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"}
@@ -5,27 +5,31 @@ exports.loadImage = void 0;
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(lazyImage) {
9
- const imageUrl = getImageUrl(lazyImage);
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 (!lazyImage.hasAttribute('src')) {
14
- lazyImage.style.opacity = '0';
14
+ if (!img.hasAttribute('src')) {
15
+ img.style.opacity = '0';
15
16
  }
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);
17
+ preloadAndFadeInImage(img, imageUrl);
18
+ if (isImageElement(img)) {
19
+ img.setAttribute('src', imageUrl);
24
20
  return;
25
21
  }
26
- lazyImage.style.backgroundImage = `url(${imageUrl})`;
22
+ img.style.backgroundImage = `url(${imageUrl})`;
27
23
  }
28
24
  exports.loadImage = loadImage;
25
+ function preloadAndFadeInImage(img, imageUrl) {
26
+ const preloadedImage = new Image();
27
+ preloadedImage.addEventListener('load', () => {
28
+ img.style.transition = 'opacity 777ms';
29
+ img.style.opacity = '1';
30
+ });
31
+ preloadedImage.src = imageUrl;
32
+ }
29
33
  const isImageElement = (img) => img.tagName === 'IMG';
30
34
  const getImageUrl = (img) => img.getAttribute('data-src');
31
35
  //# sourceMappingURL=imageLoader.js.map
@@ -1 +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"}
1
+ {"version":3,"file":"imageLoader.js","sourceRoot":"","sources":["../../src/imageLoader.ts"],"names":[],"mappings":";;;AAEA;;;GAGG;AACH,SAAgB,SAAS,CAAC,GAAiB;IACvC,MAAM,QAAQ,GAAG,WAAW,CAAC,GAAG,CAAC,CAAA;IAEjC,IAAI,CAAC,QAAQ,EAAE;QACX,OAAO,CAAC,IAAI,CAAC,wDAAwD,CAAC,CAAA;QACtE,OAAM;KACT;IAED,IAAI,CAAC,GAAG,CAAC,YAAY,CAAC,KAAK,CAAC,EAAE;QAC1B,GAAG,CAAC,KAAK,CAAC,OAAO,GAAG,GAAG,CAAA;KAC1B;IAED,qBAAqB,CAAC,GAAG,EAAE,QAAQ,CAAC,CAAA;IAEpC,IAAI,cAAc,CAAC,GAAG,CAAC,EAAE;QACrB,GAAG,CAAC,YAAY,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAA;QACjC,OAAM;KACT;IAED,GAAG,CAAC,KAAK,CAAC,eAAe,GAAG,OAAO,QAAQ,GAAG,CAAA;AAClD,CAAC;AApBD,8BAoBC;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"}
@@ -1 +1 @@
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"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,EAAgB,UAAU,EAAE,MAAM,SAAS,CAAA;oCAE/B,UAAU,WAAW,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 handler_1 = __importDefault(require("./handler"));
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, handler_1.default)(images, configurations);
16
+ return (0, observerCreator_1.default)(images, configurations);
17
17
  };
18
18
  function getImagesElements(selector) {
19
19
  if (!selector) {
@@ -1 +1 @@
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"}
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;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,yBAAe,EAAC,MAAM,EAAE,cAAc,CAAC,CAAA;AAClD,CAAC,CAAA;AAED,SAAS,iBAAiB,CAAC,QAAqB;IAC5C,IAAI,CAAC,QAAQ,EAAE;QACX,MAAM,QAAQ,GAAG,QAAQ,CAAC,gBAAgB,CAAe,0BAA0B,CAAC,CAAA;QACpF,OAAO,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAA;KAC9B;IAED,IAAI,OAAO,QAAQ,KAAK,QAAQ,EAAE;QAC9B,MAAM,QAAQ,GAAG,QAAQ,CAAC,gBAAgB,CAAe,QAAQ,CAAC,CAAA;QAClE,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"}
@@ -1,8 +1,8 @@
1
- import { Config, LazyImage } from './types';
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: LazyImage[], config: Config): void;
8
- //# sourceMappingURL=handler.d.ts.map
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
+ 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
+ for (const img of images) {
11
+ if (browserSupportsIntersectionObserver()) {
12
+ applyIntersectionObserverTo(img, config);
13
+ continue;
14
+ }
15
+ (0, imageLoader_1.loadImage)(img);
16
+ }
17
+ }
18
+ exports.default = default_1;
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":";;AACA,+CAAyC;AAEzC;;;;GAIG;AACH,mBAAyB,MAAsB,EAAE,MAAc;IAC3D,KAAK,MAAM,GAAG,IAAI,MAAM,EAAE;QACtB,IAAI,mCAAmC,EAAE,EAAE;YACvC,2BAA2B,CAAC,GAAG,EAAE,MAAM,CAAC,CAAA;YACxC,SAAQ;SACX;QAED,IAAA,uBAAS,EAAC,GAAG,CAAC,CAAA;KACjB;AACL,CAAC;AATD,4BASC;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;YACzB,oDAAoD;YACpD,IAAI,CAAC,KAAK,CAAC,cAAc,EAAE;gBACvB,SAAQ;aACX;YAED,IAAA,uBAAS,EAAC,GAAG,CAAC,CAAA;YAEd,yCAAyC;YACzC,QAAQ,CAAC,SAAS,CAAC,GAAG,CAAC,CAAA;SAC1B;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 declare type LazyImage = HTMLDivElement | HTMLImageElement;
2
- export declare type InputTypes = NodeListOf<LazyImage> | LazyImage[] | string;
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
@@ -1 +1 @@
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"}
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,6 +1,6 @@
1
1
  {
2
2
  "name": "smooth-loader",
3
- "version": "1.1.3",
3
+ "version": "1.1.4",
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",
@@ -1,36 +1,41 @@
1
- import { LazyImage } from './types'
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(lazyImage: LazyImage): void {
8
- const imageUrl = getImageUrl(lazyImage)
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 (!lazyImage.hasAttribute('src')) {
15
- lazyImage.style.opacity = '0'
15
+ if (!img.hasAttribute('src')) {
16
+ img.style.opacity = '0'
16
17
  }
17
18
 
18
- const ghostImage = new Image()
19
+ preloadAndFadeInImage(img, imageUrl)
19
20
 
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)
21
+ if (isImageElement(img)) {
22
+ img.setAttribute('src', imageUrl)
29
23
  return
30
24
  }
31
25
 
32
- lazyImage.style.backgroundImage = `url(${imageUrl})`
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: LazyImage) => img.tagName === 'IMG'
36
- const getImageUrl = (img: LazyImage) => img.getAttribute('data-src')
40
+ const isImageElement = (img: ImageElement) => img.tagName === 'IMG'
41
+ const getImageUrl = (img: ImageElement) => img.getAttribute('data-src')
package/src/index.ts CHANGED
@@ -1,5 +1,5 @@
1
- import handler from './handler'
2
- import { Config, LazyImage, InputTypes } from './types'
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)
@@ -13,17 +13,17 @@ export default (selector?: InputTypes, config?: Config): void => {
13
13
  threshold: 0,
14
14
  }
15
15
 
16
- return handler(images, configurations)
16
+ return observerCreator(images, configurations)
17
17
  }
18
18
 
19
- function getImagesElements(selector?: InputTypes): LazyImage[] {
19
+ function getImagesElements(selector?: InputTypes): ImageElement[] {
20
20
  if (!selector) {
21
- const elements = document.querySelectorAll<LazyImage>('.smooth-loader[data-src]')
21
+ const elements = document.querySelectorAll<ImageElement>('.smooth-loader[data-src]')
22
22
  return Array.from(elements)
23
23
  }
24
24
 
25
25
  if (typeof selector === 'string') {
26
- const elements = document.querySelectorAll<LazyImage>(selector)
26
+ const elements = document.querySelectorAll<ImageElement>(selector)
27
27
  return Array.from(elements)
28
28
  }
29
29
 
@@ -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 LazyImage = HTMLDivElement | HTMLImageElement
2
- export type InputTypes = NodeListOf<LazyImage> | LazyImage[] | string
1
+ export type ImageElement = HTMLDivElement | HTMLImageElement
2
+ export type InputTypes = NodeListOf<ImageElement> | ImageElement[] | string
3
3
 
4
4
  export interface Config extends IntersectionObserverInit { }
@@ -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"}
@@ -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
@@ -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"}
@@ -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
@@ -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"}
@@ -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
@@ -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
@@ -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
- }