smooth-loader 1.1.0-beta.0 → 1.1.0-beta.2
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 +17 -1
- package/README.md +2 -0
- package/lib/js/LazyLoader.d.ts.map +1 -1
- package/lib/js/LazyLoader.js +1 -5
- package/lib/js/LazyLoader.js.map +1 -1
- package/package.json +1 -1
- package/src/LazyLoader.ts +34 -27
package/CHANGELOG.md
CHANGED
|
@@ -1,6 +1,22 @@
|
|
|
1
1
|
# Release Notes
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
----
|
|
4
|
+
|
|
5
|
+
## v1.1.0-beta.2 (2022-11-30)
|
|
6
|
+
|
|
7
|
+
- Code refactoring;
|
|
8
|
+
- Manual testing;
|
|
9
|
+
|
|
10
|
+
----
|
|
11
|
+
|
|
12
|
+
## v1.1.0-beta.1 (2022-11-29)
|
|
13
|
+
|
|
14
|
+
- Removed blur from small placeholder images if they are set with `src` attribute;
|
|
15
|
+
- Added more info to docs;
|
|
16
|
+
|
|
17
|
+
----
|
|
18
|
+
|
|
19
|
+
## v1.1.0-beta.0 (2022-11-29)
|
|
4
20
|
|
|
5
21
|
- Added missing information to the README.md file;
|
|
6
22
|
- Added ability to show small images as placeholder, so you don't look at empty screen but blurred mini version of the image;
|
package/README.md
CHANGED
|
@@ -78,6 +78,8 @@ I want to load a small image on page load, and when person scrolls into the imag
|
|
|
78
78
|
|
|
79
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
80
|
|
|
81
|
+
Instead of the small version of your image you can use just a small placeholder image, and it will work fine.
|
|
82
|
+
|
|
81
83
|
## ⚙️ Configurations
|
|
82
84
|
|
|
83
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).
|
|
@@ -1 +1 @@
|
|
|
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;
|
|
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
|
@@ -17,17 +17,13 @@ class default_1 {
|
|
|
17
17
|
const imageUrl = img.getAttribute('data-src');
|
|
18
18
|
if (!imageUrl)
|
|
19
19
|
return;
|
|
20
|
-
if (img.hasAttribute('src')) {
|
|
21
|
-
img.style.filter = 'blur(7px)';
|
|
22
|
-
}
|
|
23
|
-
else {
|
|
20
|
+
if (!img.hasAttribute('src')) {
|
|
24
21
|
img.style.opacity = '0';
|
|
25
22
|
}
|
|
26
23
|
const ghostImage = new Image();
|
|
27
24
|
ghostImage.addEventListener('load', () => {
|
|
28
25
|
img.style.transition = 'opacity 777ms';
|
|
29
26
|
img.style.opacity = '1';
|
|
30
|
-
img.style.filter = 'none';
|
|
31
27
|
});
|
|
32
28
|
ghostImage.src = imageUrl;
|
|
33
29
|
img.tagName === 'IMG'
|
package/lib/js/LazyLoader.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
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,GAAG,CAAC,YAAY,CAAC,KAAK,CAAC,EAAE;
|
|
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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "smooth-loader",
|
|
3
|
-
"version": "1.1.0-beta.
|
|
3
|
+
"version": "1.1.0-beta.2",
|
|
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",
|
package/src/LazyLoader.ts
CHANGED
|
@@ -15,49 +15,54 @@ export default class {
|
|
|
15
15
|
* If image has tag IMG then set the src attribute to img url,
|
|
16
16
|
* otherwise set the background of the element to given image url
|
|
17
17
|
*/
|
|
18
|
-
private loadImage(
|
|
19
|
-
const imageUrl =
|
|
18
|
+
private loadImage(lazyImage: LazyImage): void {
|
|
19
|
+
const imageUrl = lazyImage.getAttribute('data-src')
|
|
20
20
|
|
|
21
|
-
if (!imageUrl)
|
|
21
|
+
if (!imageUrl) {
|
|
22
22
|
return
|
|
23
|
+
}
|
|
23
24
|
|
|
24
|
-
if (
|
|
25
|
-
|
|
26
|
-
} else {
|
|
27
|
-
img.style.opacity = '0'
|
|
25
|
+
if (!lazyImage.hasAttribute('src')) {
|
|
26
|
+
lazyImage.style.opacity = '0'
|
|
28
27
|
}
|
|
29
28
|
|
|
30
29
|
const ghostImage = new Image()
|
|
31
30
|
|
|
32
31
|
ghostImage.addEventListener('load', () => {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
img.style.filter = 'none'
|
|
32
|
+
lazyImage.style.transition = 'opacity 777ms'
|
|
33
|
+
lazyImage.style.opacity = '1'
|
|
36
34
|
})
|
|
37
35
|
|
|
38
36
|
ghostImage.src = imageUrl
|
|
39
37
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
38
|
+
if (lazyImage.tagName === 'IMG') {
|
|
39
|
+
lazyImage.setAttribute('src', imageUrl)
|
|
40
|
+
return
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
lazyImage.style.backgroundImage = `url(${imageUrl})`
|
|
43
44
|
}
|
|
44
45
|
|
|
45
46
|
|
|
46
47
|
/**
|
|
47
|
-
*
|
|
48
|
-
*
|
|
48
|
+
* Creates instance of IntersectionObserver and loads image in DOM
|
|
49
|
+
* as soon as image will be visible on the screen
|
|
49
50
|
*/
|
|
50
|
-
private createObserver(
|
|
51
|
-
const
|
|
52
|
-
|
|
53
|
-
if (entry.isIntersecting) {
|
|
54
|
-
|
|
55
|
-
observer.unobserve(img)
|
|
51
|
+
private createObserver(lazyImage: LazyImage): void {
|
|
52
|
+
const handleObserver = (entries: IntersectionObserverEntry[], observer: IntersectionObserver) => {
|
|
53
|
+
for (const entry of entries) {
|
|
54
|
+
if (!entry.isIntersecting) {
|
|
55
|
+
continue
|
|
56
56
|
}
|
|
57
|
-
})
|
|
58
|
-
}, this.config)
|
|
59
57
|
|
|
60
|
-
|
|
58
|
+
this.loadImage(lazyImage)
|
|
59
|
+
observer.unobserve(lazyImage)
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const observer = new IntersectionObserver(handleObserver, this.config)
|
|
64
|
+
|
|
65
|
+
observer.observe(lazyImage)
|
|
61
66
|
}
|
|
62
67
|
|
|
63
68
|
/**
|
|
@@ -66,8 +71,10 @@ export default class {
|
|
|
66
71
|
* when image is loaded, appends it to a placeholder
|
|
67
72
|
*/
|
|
68
73
|
public execute(): void {
|
|
69
|
-
this.images
|
|
70
|
-
|
|
71
|
-
|
|
74
|
+
for (const img of this.images) {
|
|
75
|
+
window['IntersectionObserver']
|
|
76
|
+
? this.createObserver(img)
|
|
77
|
+
: this.loadImage(img)
|
|
78
|
+
}
|
|
72
79
|
}
|
|
73
80
|
}
|