smooth-loader 1.1.0-beta.1 → 1.1.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/CHANGELOG.md +13 -0
- package/package.json +1 -1
- package/src/LazyLoader.ts +34 -24
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,19 @@
|
|
|
2
2
|
|
|
3
3
|
----
|
|
4
4
|
|
|
5
|
+
## v1.1.0 (2022-12-01)
|
|
6
|
+
|
|
7
|
+
- Moved from beta version;
|
|
8
|
+
|
|
9
|
+
----
|
|
10
|
+
|
|
11
|
+
## v1.1.0-beta.2 (2022-11-30)
|
|
12
|
+
|
|
13
|
+
- Code refactoring;
|
|
14
|
+
- Manual testing;
|
|
15
|
+
|
|
16
|
+
----
|
|
17
|
+
|
|
5
18
|
## v1.1.0-beta.1 (2022-11-29)
|
|
6
19
|
|
|
7
20
|
- Removed blur from small placeholder images if they are set with `src` attribute;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "smooth-loader",
|
|
3
|
-
"version": "1.1.0
|
|
3
|
+
"version": "1.1.0",
|
|
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,46 +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
|
-
|
|
25
|
+
if (!lazyImage.hasAttribute('src')) {
|
|
26
|
+
lazyImage.style.opacity = '0'
|
|
26
27
|
}
|
|
27
28
|
|
|
28
29
|
const ghostImage = new Image()
|
|
29
30
|
|
|
30
31
|
ghostImage.addEventListener('load', () => {
|
|
31
|
-
|
|
32
|
-
|
|
32
|
+
lazyImage.style.transition = 'opacity 777ms'
|
|
33
|
+
lazyImage.style.opacity = '1'
|
|
33
34
|
})
|
|
34
35
|
|
|
35
36
|
ghostImage.src = imageUrl
|
|
36
37
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
38
|
+
if (lazyImage.tagName === 'IMG') {
|
|
39
|
+
lazyImage.setAttribute('src', imageUrl)
|
|
40
|
+
return
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
lazyImage.style.backgroundImage = `url(${imageUrl})`
|
|
40
44
|
}
|
|
41
45
|
|
|
42
46
|
|
|
43
47
|
/**
|
|
44
|
-
*
|
|
45
|
-
*
|
|
48
|
+
* Creates instance of IntersectionObserver and loads image in DOM
|
|
49
|
+
* as soon as image will be visible on the screen
|
|
46
50
|
*/
|
|
47
|
-
private createObserver(
|
|
48
|
-
const
|
|
49
|
-
|
|
50
|
-
if (entry.isIntersecting) {
|
|
51
|
-
|
|
52
|
-
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
|
|
53
56
|
}
|
|
54
|
-
})
|
|
55
|
-
}, this.config)
|
|
56
57
|
|
|
57
|
-
|
|
58
|
+
this.loadImage(lazyImage)
|
|
59
|
+
observer.unobserve(lazyImage)
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const observer = new IntersectionObserver(handleObserver, this.config)
|
|
64
|
+
|
|
65
|
+
observer.observe(lazyImage)
|
|
58
66
|
}
|
|
59
67
|
|
|
60
68
|
/**
|
|
@@ -63,8 +71,10 @@ export default class {
|
|
|
63
71
|
* when image is loaded, appends it to a placeholder
|
|
64
72
|
*/
|
|
65
73
|
public execute(): void {
|
|
66
|
-
this.images
|
|
67
|
-
|
|
68
|
-
|
|
74
|
+
for (const img of this.images) {
|
|
75
|
+
window['IntersectionObserver']
|
|
76
|
+
? this.createObserver(img)
|
|
77
|
+
: this.loadImage(img)
|
|
78
|
+
}
|
|
69
79
|
}
|
|
70
80
|
}
|