smooth-loader 1.0.7 → 1.1.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +16 -0
- package/README.md +43 -0
- package/lib/js/LazyLoader.d.ts +3 -3
- package/lib/js/LazyLoader.d.ts.map +1 -1
- package/lib/js/LazyLoader.js +6 -7
- package/lib/js/LazyLoader.js.map +1 -1
- package/lib/js/index.d.ts +2 -2
- package/lib/js/index.d.ts.map +1 -1
- package/lib/js/index.js +6 -2
- package/lib/js/index.js.map +1 -1
- package/lib/js/types.d.ts +2 -0
- package/lib/js/types.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/LazyLoader.ts +3 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,21 @@
|
|
|
1
1
|
# Release Notes
|
|
2
2
|
|
|
3
|
+
----
|
|
4
|
+
|
|
5
|
+
## v1.1.0-beta.1 (2022-11-29)
|
|
6
|
+
|
|
7
|
+
- Removed blur from small placeholder images if they are set with `src` attribute;
|
|
8
|
+
- Added more info to docs;
|
|
9
|
+
|
|
10
|
+
----
|
|
11
|
+
|
|
12
|
+
## v1.1.0-beta.0 (2022-11-29)
|
|
13
|
+
|
|
14
|
+
- Added missing information to the README.md file;
|
|
15
|
+
- Added ability to show small images as placeholder, so you don't look at empty screen but blurred mini version of the image;
|
|
16
|
+
|
|
17
|
+
----
|
|
18
|
+
|
|
3
19
|
## v1.0.7 (2022-06-22)
|
|
4
20
|
|
|
5
21
|
- Fixed appearing transition with background images. Now it works.
|
package/README.md
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
- [📄 Description](#-description)
|
|
3
3
|
- [🎁 Features](#-features)
|
|
4
4
|
- [✍️ Usage](#%EF%B8%8F-usage)
|
|
5
|
+
- [🌄 Image preview](#-image-preview)
|
|
6
|
+
- [⚙️ Configurations](#%EF%B8%8F-configurations)
|
|
5
7
|
- [🚀️ Quick start](#-quick-start)
|
|
6
8
|
- Useful links
|
|
7
9
|
- [NPM](https://www.npmjs.com/package/smooth-loader)
|
|
@@ -53,6 +55,47 @@ smoothLoader(myImages)
|
|
|
53
55
|
<div data-src="./images/we.png" class="smooth-loader my-bg-image"></div>
|
|
54
56
|
```
|
|
55
57
|
|
|
58
|
+
## 🌄 Image preview
|
|
59
|
+
|
|
60
|
+
You can add a small version of the image to `src` attribute on image element, 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.
|
|
61
|
+
|
|
62
|
+
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:
|
|
63
|
+
|
|
64
|
+

|
|
65
|
+
|
|
66
|
+
I want to load a small image on 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.
|
|
67
|
+
|
|
68
|
+
```html
|
|
69
|
+
<img
|
|
70
|
+
alt="Beautiful girl"
|
|
71
|
+
class="smooth-loader"
|
|
72
|
+
src="images/1min.jpg"
|
|
73
|
+
data-src="images/1.jpg"
|
|
74
|
+
width="736"
|
|
75
|
+
height="725"
|
|
76
|
+
/>
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
> 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.
|
|
80
|
+
|
|
81
|
+
Instead of the small version of your image you can use just a small placeholder image, and it will work fine.
|
|
82
|
+
|
|
83
|
+
## ⚙️ Configurations
|
|
84
|
+
|
|
85
|
+
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).
|
|
86
|
+
|
|
87
|
+
```typescript
|
|
88
|
+
smoothLoader('.lazy-image', {
|
|
89
|
+
root: document.querySelector('#scrollArea'),
|
|
90
|
+
rootMargin: '0px',
|
|
91
|
+
threshold: 1.0,
|
|
92
|
+
})
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
- `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.
|
|
96
|
+
- `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.
|
|
97
|
+
- `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.
|
|
98
|
+
|
|
56
99
|
## 🚀 Quick Start
|
|
57
100
|
|
|
58
101
|
```bash
|
package/lib/js/LazyLoader.d.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import { LazyImage } from './types';
|
|
1
|
+
import { Config, LazyImage } from './types';
|
|
2
2
|
/**
|
|
3
3
|
* Class that handles lazy loading images with
|
|
4
4
|
* Intersection Observer
|
|
5
5
|
*/
|
|
6
6
|
export default class {
|
|
7
7
|
private readonly images;
|
|
8
|
-
private readonly
|
|
9
|
-
constructor(images: NodeListOf<LazyImage
|
|
8
|
+
private readonly config;
|
|
9
|
+
constructor(images: NodeListOf<LazyImage>, config: Config);
|
|
10
10
|
/**
|
|
11
11
|
* If image has tag IMG then set the src attribute to img url,
|
|
12
12
|
* otherwise set the background of the element to given image url
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"LazyLoader.d.ts","sourceRoot":"","sources":["../../src/LazyLoader.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,SAAS,CAAA;
|
|
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
CHANGED
|
@@ -5,12 +5,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
5
5
|
* Intersection Observer
|
|
6
6
|
*/
|
|
7
7
|
class default_1 {
|
|
8
|
-
constructor(images) {
|
|
8
|
+
constructor(images, config) {
|
|
9
9
|
this.images = images;
|
|
10
|
-
this.
|
|
11
|
-
root: null,
|
|
12
|
-
threshold: 0,
|
|
13
|
-
};
|
|
10
|
+
this.config = config;
|
|
14
11
|
}
|
|
15
12
|
/**
|
|
16
13
|
* If image has tag IMG then set the src attribute to img url,
|
|
@@ -20,7 +17,9 @@ class default_1 {
|
|
|
20
17
|
const imageUrl = img.getAttribute('data-src');
|
|
21
18
|
if (!imageUrl)
|
|
22
19
|
return;
|
|
23
|
-
img.
|
|
20
|
+
if (!img.hasAttribute('src')) {
|
|
21
|
+
img.style.opacity = '0';
|
|
22
|
+
}
|
|
24
23
|
const ghostImage = new Image();
|
|
25
24
|
ghostImage.addEventListener('load', () => {
|
|
26
25
|
img.style.transition = 'opacity 777ms';
|
|
@@ -43,7 +42,7 @@ class default_1 {
|
|
|
43
42
|
observer.unobserve(img);
|
|
44
43
|
}
|
|
45
44
|
});
|
|
46
|
-
}, this.
|
|
45
|
+
}, this.config);
|
|
47
46
|
observer.observe(img);
|
|
48
47
|
}
|
|
49
48
|
/**
|
package/lib/js/LazyLoader.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"LazyLoader.js","source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AAI,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/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { LazyImage } from './types';
|
|
2
|
-
declare const _default: (selector?:
|
|
1
|
+
import { Config, LazyImage } from './types';
|
|
2
|
+
declare const _default: (selector?: NodeListOf<LazyImage> | string, config?: Config) => void;
|
|
3
3
|
export default _default;
|
|
4
4
|
//# sourceMappingURL=index.d.ts.map
|
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,SAAS,EAAE,MAAM,SAAS,CAAA;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,SAAS,CAAA;oCAEhB,WAAW,SAAS,CAAC,GAAG,MAAM,WAAW,MAAM,KAAG,IAAI;AAAjF,wBAYC"}
|
package/lib/js/index.js
CHANGED
|
@@ -4,11 +4,15 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
const LazyLoader_1 = __importDefault(require("./LazyLoader"));
|
|
7
|
-
exports.default = (selector) => {
|
|
7
|
+
exports.default = (selector, config) => {
|
|
8
8
|
const images = getImagesElements(selector);
|
|
9
9
|
if (!images)
|
|
10
10
|
return;
|
|
11
|
-
|
|
11
|
+
const configurations = config || {
|
|
12
|
+
root: null,
|
|
13
|
+
threshold: 0,
|
|
14
|
+
};
|
|
15
|
+
return new LazyLoader_1.default(images, configurations).execute();
|
|
12
16
|
};
|
|
13
17
|
function getImagesElements(selector) {
|
|
14
18
|
if (typeof selector === 'string' && selector !== '')
|
package/lib/js/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;AAAA,8DAAqC;AAGrC,kBAAe,CAAC,QAAyC,EAAQ,EAAE;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;AAAA,8DAAqC;AAGrC,kBAAe,CAAC,QAAyC,EAAE,MAAe,EAAQ,EAAE;IAChF,MAAM,MAAM,GAAG,iBAAiB,CAAC,QAAQ,CAAC,CAAA;IAE1C,IAAI,CAAC,MAAM;QACP,OAAM;IAEV,MAAM,cAAc,GAAG,MAAM,IAAI;QAC7B,IAAI,EAAE,IAAI;QACV,SAAS,EAAE,CAAC;KACf,CAAA;IAED,OAAO,IAAI,oBAAU,CAAC,MAAM,EAAE,cAAc,CAAC,CAAC,OAAO,EAAE,CAAA;AAC3D,CAAC,CAAA;AAED,SAAS,iBAAiB,CAAC,QAAyC;IAChE,IAAI,OAAO,QAAQ,KAAK,QAAQ,IAAI,QAAQ,KAAK,EAAE;QAC/C,OAAO,QAAQ,CAAC,gBAAgB,CAAY,QAAQ,CAAC,CAAA;IAEzD,IAAI,QAAQ,YAAY,QAAQ;QAC5B,OAAO,QAAQ,CAAA;IAEnB,OAAO,QAAQ,CAAC,gBAAgB,CAAY,0BAA0B,CAAC,CAAA;AAC3E,CAAC"}
|
package/lib/js/types.d.ts
CHANGED
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,oBAAY,SAAS,GAAG,cAAc,GAAG,gBAAgB,CAAA"}
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"AAAA,oBAAY,SAAS,GAAG,cAAc,GAAG,gBAAgB,CAAA;AAEzD,MAAM,WAAW,MAAO,SAAQ,wBAAwB;CACvD"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "smooth-loader",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.1.0-beta.1",
|
|
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",
|