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 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-beta.1",
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(img: LazyImage): void {
19
- const imageUrl = img.getAttribute('data-src')
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 (!img.hasAttribute('src')) {
25
- img.style.opacity = '0'
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
- img.style.transition = 'opacity 777ms'
32
- img.style.opacity = '1'
32
+ lazyImage.style.transition = 'opacity 777ms'
33
+ lazyImage.style.opacity = '1'
33
34
  })
34
35
 
35
36
  ghostImage.src = imageUrl
36
37
 
37
- img.tagName === 'IMG'
38
- ? img.setAttribute('src', imageUrl)
39
- : img.style.backgroundImage = `url(${imageUrl})`
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
- * Create observer object that will trigger loading image function
45
- * when it's gonna be visible on the screen
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(img: LazyImage): void {
48
- const observer = new IntersectionObserver((entries, observer) => {
49
- entries.forEach(entry => {
50
- if (entry.isIntersecting) {
51
- this.loadImage(img)
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
- observer.observe(img)
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.forEach(img => {
67
- !window['IntersectionObserver'] ? this.loadImage(img) : this.createObserver(img)
68
- })
74
+ for (const img of this.images) {
75
+ window['IntersectionObserver']
76
+ ? this.createObserver(img)
77
+ : this.loadImage(img)
78
+ }
69
79
  }
70
80
  }