@@ -34,15 +34,15 @@ const { threshold = 0, percentage = false, rootMargin = 0 } = Astro.props
34
34
.clone-container {
35
35
position: absolute;
36
36
z-index: -1;
37
- background-color: rgba(0, 0, 0, .1);
37
+ background-color: rgba(0, 0, 0, 0 .1);
38
38
left: 0;
39
39
right: 0;
40
40
border: 1px solid white;
41
41
bottom: 0;
42
42
}
43
43
44
44
:root[data-theme="dark"] .clone-container {
45
- background-color: rgba(255, 255, 255, .1);
45
+ background-color: rgba(255, 255, 255, 0 .1);
46
46
}
47
47
48
48
.root-margin-lines {
@@ -58,14 +58,27 @@ const { threshold = 0, percentage = false, rootMargin = 0 } = Astro.props
58
58
}
59
59
</style >
60
60
61
- <div data-intersection-observer-component class =" container" style ={ ` padding-top: ${WINDOW_HEIGHT - VIEWPORT_HEIGHT }px; padding-bottom: ${WINDOW_HEIGHT - VIEWPORT_HEIGHT }px; ` } >
61
+ <div
62
+ data-intersection-observer-component
63
+ class =" container"
64
+ style ={ ` padding-top: ${WINDOW_HEIGHT - VIEWPORT_HEIGHT }px; padding-bottom: ${
65
+ WINDOW_HEIGHT - VIEWPORT_HEIGHT
66
+ }px; ` }
67
+ >
62
68
<div
63
69
data-scroll-element
64
70
class =" scroll-element"
65
71
style ={ ` height: ${VIEWPORT_HEIGHT }px; ` }
66
72
>
67
73
<div style ={ ` height: ${WINDOW_HEIGHT }px; padding-top: ${MARGIN }px; ` } >
68
- <div data-main-element class =" main-element" data-threshold ={ threshold } data-percentage ={ percentage .toString ()} data-root-margin ={ rootMargin } ></div >
74
+ <div
75
+ data-main-element
76
+ class =" main-element"
77
+ data-threshold ={ threshold }
78
+ data-percentage ={ percentage .toString ()}
79
+ data-root-margin ={ rootMargin }
80
+ >
81
+ </div >
69
82
</div >
70
83
</div >
71
84
<div
@@ -75,53 +88,54 @@ const { threshold = 0, percentage = false, rootMargin = 0 } = Astro.props
75
88
>
76
89
<div data-clone-element class =" clone-element" ></div >
77
90
</div >
78
- { rootMargin !== 0 && (
91
+ {
92
+ rootMargin !== 0 && (
79
93
<div class = " root-margin-lines" >
80
- <div class = " line" style = { ` top: ${rootMargin }px ` } ></div >
81
- <div class = " line" style = { ` bottom: ${VIEWPORT_HEIGHT + rootMargin }px ` } ></div >
82
- </div >
83
- )}
94
+ <div class = " line" style = { ` top: ${rootMargin }px ` } />
95
+ <div class = " line" style = { ` bottom: ${VIEWPORT_HEIGHT + rootMargin }px ` } />
96
+ </div >
97
+ )
98
+ }
84
99
</div >
85
100
86
101
<script >
87
- if (document.readyState === "complete" || document.readyState === "interactive") {
88
- ready()
89
- } else {
90
- document.addEventListener("DOMContentLoaded", ready)
91
- }
102
+ const containers = document.querySelectorAll(
103
+ "[data-intersection-observer-component]"
104
+ )
105
+ containers.forEach(container => {
106
+ const mainElement = container.querySelector("[data-main-element]")
107
+ const cloneElement = container.querySelector("[data-clone-element]")
108
+ const cloneContainer = container.querySelector("[data-clone-container]")
109
+ const scrollElement = container.querySelector("[data-scroll-element]")
92
110
93
- function ready() {
94
- const containers = document.querySelectorAll("[data-intersection-observer-component]")
95
- containers.forEach(container => {
96
- const mainElement = container.querySelector("[data-main-element]")
97
- const cloneElement = container.querySelector("[data-clone-element]")
98
- const cloneContainer = container.querySelector("[data-clone-container]")
99
- const scrollElement = container.querySelector("[data-scroll-element]")
100
-
101
- const threshold = JSON.parse(mainElement.dataset.threshold)
102
- const percentage = JSON.parse(mainElement.dataset.percentage)
103
- const rootMargin = JSON.parse(mainElement.dataset.rootMargin)
104
- const observerCallback = ({ intersectionRatio, isIntersecting }, elements) => {
105
- if (percentage) {
106
- elements.forEach(element => {
107
- element.textContent = `${Math.round(intersectionRatio * 100)}%`
108
- })
109
- } else {
110
- elements.forEach(element => {
111
- element.style.backgroundColor = isIntersecting ? "var(--theme-blue)" : "var(--theme-orange)"
112
- })
113
- }
111
+ const threshold = JSON.parse(mainElement.dataset.threshold)
112
+ const percentage = JSON.parse(mainElement.dataset.percentage)
113
+ const rootMargin = JSON.parse(mainElement.dataset.rootMargin)
114
+ const observerCallback = (
115
+ { intersectionRatio, isIntersecting },
116
+ elements
117
+ ) => {
118
+ if (percentage) {
119
+ elements.forEach(element => {
120
+ element.textContent = `${Math.round(intersectionRatio * 100)}%`
121
+ })
122
+ } else {
123
+ elements.forEach(element => {
124
+ element.style.backgroundColor = isIntersecting
125
+ ? "var(--theme-blue)"
126
+ : "var(--theme-orange)"
127
+ })
114
128
}
129
+ }
115
130
116
- scrollElement.addEventListener("scroll", e => {
117
- cloneContainer.style.bottom = `${e.target.scrollTop}px`
118
- })
119
- console.log(`${rootMargin}px`)
120
- const observer = new IntersectionObserver(
121
- entries => observerCallback(entries[0], [mainElement, cloneElement]),
122
- { threshold, rootMargin: `${rootMargin}px`, root: scrollElement }
123
- )
124
- observer.observe(mainElement)
131
+ scrollElement.addEventListener("scroll", e => {
132
+ cloneContainer.style.bottom = `${e.target.scrollTop}px`
125
133
})
126
- }
127
- </script >
134
+ console.log(`${rootMargin}px`)
135
+ const observer = new IntersectionObserver(
136
+ entries => observerCallback(entries[0], [mainElement, cloneElement]),
137
+ { threshold, rootMargin: `${rootMargin}px`, root: scrollElement }
138
+ )
139
+ observer.observe(mainElement)
140
+ })
141
+ </script >
0 commit comments