@@ -119,28 +119,28 @@ const listItemClasses = "flex items-center gap-2.5 text-[22px] leading-8";
119119 }
120120 </div >
121121 <script >
122- const screenHeight = window.innerHeight;
122+ const getObserverOpts = () => {
123+ const screenHeight = window.innerHeight;
123124
124- const observingElemsHeight = 700;
125+ const observingElemsHeight = 700;
125126
126- // Intersection area: from the very top of viewport
127- // down to observed elements height (700px) x 1.5
128- let intersectionBottomMargin = screenHeight - observingElemsHeight * 1.5;
129- let rootMargin = "0px";
127+ // Intersection area: from the very top of viewport
128+ // down to observed elements height (700px) x 1.5
129+ let intersectionBottomMargin = screenHeight - observingElemsHeight * 1.5;
130+ let rootMargin = "0px";
130131
131- if (intersectionBottomMargin > 0) {
132- rootMargin = `0px 0px -${intersectionBottomMargin}px 0px`;
133- }
132+ if (intersectionBottomMargin > 0) {
133+ rootMargin = `0px 0px -${intersectionBottomMargin}px 0px`;
134+ }
134135
135- const opts = {
136- root: null,
137- rootMargin: rootMargin,
138- threshold: [0.7],
136+ return {
137+ root: null,
138+ rootMargin: rootMargin,
139+ threshold: [0.7],
140+ };
139141 };
140142
141- const images = document.querySelectorAll(".images");
142-
143- const observer = new IntersectionObserver((entries) => {
143+ const observeCallback = (entries: IntersectionObserverEntry[]) => {
144144 entries.forEach((entry) => {
145145 if (entry.isIntersecting) {
146146 const targetIdx = Number(entry.target.getAttribute("data-index"));
@@ -155,11 +155,33 @@ const listItemClasses = "flex items-center gap-2.5 text-[22px] leading-8";
155155 });
156156 }
157157 });
158- }, opts) ;
158+ };
159159
160160 const textSections = document.querySelectorAll(".phase");
161- textSections.forEach((section) => {
162- observer.observe(section);
161+ const images = document.querySelectorAll(".images");
162+
163+ let observer = new IntersectionObserver(observeCallback, getObserverOpts());
164+
165+ const handleUnobserve = (observer: IntersectionObserver) => {
166+ textSections.forEach((section) => {
167+ observer.observe(section);
168+ });
169+ };
170+
171+ const handleObserve = () => {
172+ if (observer) {
173+ handleUnobserve(observer);
174+ }
175+ observer = new IntersectionObserver(observeCallback, getObserverOpts());
176+ textSections.forEach((section) => {
177+ observer.observe(section);
178+ });
179+ };
180+
181+ window.addEventListener("resize", () => {
182+ handleObserve();
163183 });
184+
185+ handleObserve();
164186 </script >
165187</Section >
0 commit comments