smooth-loader 1.1.2 → 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,59 +1,51 @@
1
1
  # Release Notes
2
2
 
3
- ----
3
+ ## v1.1.4 (2024-04-08)
4
4
 
5
- ## v1.1.2 (2022-12-04)
5
+ - Formatted the `CHANGELOG.md` file
6
+ - Refactor the code to make it more readable
7
+ - Improve error handling
6
8
 
7
- - Removed line from docs;
9
+ ## v1.1.3 (2023-11-19)
8
10
 
9
- ----
11
+ - Changed the `README.MD` file
10
12
 
11
- ## v1.1.1 (2022-12-04)
13
+ ## v1.1.2 (2022-12-04)
12
14
 
13
- - Code refactoring;
14
- - Added ability to provide array of images instead of only NodeListOf;
15
- - Improved documentation (README.md);
15
+ - Removed line from docs
16
16
 
17
- ----
17
+ ## v1.1.1 (2022-12-04)
18
18
 
19
- ## v1.1.0 (2022-12-01)
19
+ - Code refactoring
20
+ - Added ability to provide array of images instead of only NodeListOf
21
+ - Improved documentation in `README.md`
20
22
 
21
- - Moved from beta version;
23
+ ## v1.1.0 (2022-12-01)
22
24
 
23
- ----
25
+ - Moved from beta version
24
26
 
25
27
  ## v1.1.0-beta.2 (2022-11-30)
26
28
 
27
- - Code refactoring;
28
- - Manual testing;
29
-
30
- ----
29
+ - Code refactoring
30
+ - Manual testing
31
31
 
32
32
  ## v1.1.0-beta.1 (2022-11-29)
33
33
 
34
- - Removed blur from small placeholder images if they are set with `src` attribute;
35
- - Added more info to docs;
36
-
37
- ----
34
+ - Removed blur from small placeholder images if they are set with `src` attribute
35
+ - Added more info to docs
38
36
 
39
37
  ## v1.1.0-beta.0 (2022-11-29)
40
38
 
41
- - Added missing information to the README.md file;
42
- - Added ability to show small images as placeholder, so you don't look at empty screen but blurred mini version of the image;
43
-
44
- ----
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
45
41
 
46
42
  ## v1.0.7 (2022-06-22)
47
43
 
48
- - Fixed appearing transition with background images. Now it works.
49
- - Improved documentation `README.md` file.
50
- - Added **configurations** for smooth loading images.
51
-
52
- ----
53
-
54
- ## 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
55
47
 
56
- - Changed default images selector to be more specific.
57
- - Added `CHANGELOG.md` file.
48
+ ## v1.0.6 (2022-01-02)
58
49
 
59
- ----
50
+ - Changed default images selector to be more specific
51
+ - Added `CHANGELOG.md` file
package/README.md CHANGED
@@ -1,137 +1,7 @@
1
- - Documentation menu
2
- - [📄 Description](#-description)
3
- - [🎁 Features](#-features)
4
- - [✍️ Usage](#%EF%B8%8F-usage)
5
- - [🌄 Image preview](#-image-preview)
6
- - [⚙️ Configurations](#%EF%B8%8F-configurations)
7
- - [🚀️ Quick start](#-quick-start)
8
- - Useful links
9
- - [NPM](https://www.npmjs.com/package/smooth-loader)
10
- - [Usage on codesandbox](https://codesandbox.io/s/smooth-loader-example-usage-5xr6h)
11
- - [Example usage in production](https://shobar.com.ua/category/photo-reports)
12
-
13
- ## 📄 Description
1
+ ![Smooth loader logo](https://smooth-loader.github.io/images/logo.png)
14
2
 
15
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.
16
4
 
17
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.
18
6
 
19
- ## 🎁 Features
20
-
21
- - 😍 Well-documented;
22
- - 😍 Written in TypeScript;
23
- - 😍 Very small package;
24
- - 😍 Loads images with IMG tag and backgrounds;
25
- - 😍 Loads images with a smooth fade in animation;
26
- - 😍 For image elements, you can show minified version of the image on initial page load;
27
- - 😍 Configurable;
28
-
29
- ## ✍️ Usage
30
-
31
- To make image element or background image lazy loaded:
32
- - Add image URL to `data-src` attribute;
33
- - Call `smoothLoader()` function;
34
-
35
- #### Look at several options to choose images which you want to lazy load:
36
-
37
- ```js
38
- import smoothLoader from 'smooth-loader'
39
-
40
- // Default selector
41
- // Selects all images with 'smooth-loader' class
42
- smoothLoader()
43
-
44
- // Custom selector
45
- // Selects all images with 'lazy-image' class
46
- smoothLoader('.lazy-image')
47
-
48
- // Elements
49
- // Selects all the images
50
- const images = document.querySelectorAll<HTMLImageElement>('img')
51
- smoothLoader(images)
52
-
53
- // Certain elements
54
- // Selects all the images
55
- const myPhoto = document.querySelector<HTMLImageElement>('#me')
56
-
57
- if (myPhoto) {
58
- smoothLoader([myPhoto])
59
- }
60
- ```
61
-
62
- > Note, that lazy loading works only when your elements are already in the DOM. Meaning, if images are loaded after the `smoothLoader` was executed, lazy loading will not work.
63
-
64
- ```html
65
- <!-- Regular image -->
66
- <img
67
- src="./images/me-min.webp"
68
- data-src="./images/me.webp"
69
- class="smooth-loader"
70
- alt="Photo of Serhii Cho"
71
- />
72
-
73
- <!-- Background image -->
74
- <div
75
- data-src="./images/we.png"
76
- class="smooth-loader my-bg-image"
77
- ></div>
78
- ```
79
-
80
- ## 🌄 Image preview
81
-
82
- You can add a small version of the image to `src` attribute on image element `<img src="here">`, so that users don't look at the empty place when image is loading. It can be useful if you have many images on the page or you have several large images which.
83
-
84
- To use this feature, you'll need small versions of your images which will be loaded right away when the page loads. Let's say I have 2 same images with different sizes:
85
-
86
- ![Big and small images](https://serhii.io/storage/other/big-and-small-images.png)
87
-
88
- I want to load a small image on the initial page load, and when person scrolls into the image view, the bigger version will be loaded and it will replace the small one. In my HTML I will simply add an `src` attribute with path to a small image.
89
-
90
- ```html
91
- <img
92
- alt="Beautiful girl"
93
- class="smooth-loader"
94
- src="images/1min.jpg"
95
- data-src="images/1.jpg"
96
- width="736"
97
- height="725"
98
- />
99
- ```
100
-
101
- > It's important to add `width` and `height` attributes or have width and hight setup correctly with CSS. Because small image has to be filling all the space that large image is taking.
102
-
103
- > This feature works only for image elements, background images do not support it yet!
104
-
105
- Instead of the small version of your image you can use just a small placeholder image, and it will work fine.
106
-
107
- ## ⚙️ Configurations
108
-
109
- As the second argument, you can pass configurations. Some of the parameters extend [Intersection Observer API](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API).
110
-
111
- ```typescript
112
- smoothLoader('.lazy-image', {
113
- root: document.querySelector('#scrollArea'),
114
- rootMargin: '0px',
115
- threshold: 1.0,
116
- })
117
- ```
118
-
119
- - `root`: The element that is used as the viewport for checking visibility of the target. Must be the ancestor of the target. Defaults to the browser viewport if not specified or if null.
120
- - `rootMargin`: Margin around the root. Can have values similar to the CSS margin property, e.g. "10px 20px 30px 40px" (top, right, bottom, left). The values can be percentages. This set of values serves to grow or shrink each side of the root element's bounding box before computing intersections. Defaults to all zeros.
121
- - `threshold`: Either a single number or an array of numbers which indicate at what percentage of the target's visibility the observer's callback should be executed. If you only want to detect when visibility passes the 50% mark, you can use a value of 0.5. If you want the callback to run every time visibility passes another 25%, you would specify the array [0, 0.25, 0.5, 0.75, 1]. The default is 0 (meaning as soon as even one pixel is visible, the callback will be run). A value of 1.0 means that the threshold isn't considered passed until every pixel is visible.
122
-
123
- ## 🚀 Quick Start
124
-
125
- ```bash
126
- # npm
127
- npm i smooth-loader
128
-
129
- # yarn
130
- yarn add smooth-loader
131
- ```
132
-
133
- ```js
134
- import smoothLoader from 'smooth-loader'
135
-
136
- smoothLoader()
137
- ```
7
+ ## Follow the [official documentation](https://smooth-loader.github.io) for all the details
@@ -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":"imageL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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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C,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.2",
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",
@@ -21,5 +21,5 @@
21
21
  ],
22
22
  "author": "Serhii Cho",
23
23
  "license": "MIT",
24
- "homepage": "https://github.com/SerhiiCho/smooth-loader#readme"
24
+ "homepage": "https://smooth-loader.github.io"
25
25
  }
@@ -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,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AAC,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,C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AAA;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
- }