Skip to content

Commit 91b9bd7

Browse files
committed
fix(animation): keep scroll direction across observers
1 parent e245d85 commit 91b9bd7

1 file changed

Lines changed: 11 additions & 2 deletions

File tree

‎src/js/common/classes/Animation.js‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const animationClass = 'js-animation';
1010
// one IntersectionObserver per rootMargin, shared by all instances using it
1111
const observers = new Map();
1212
const lastScroll = { x: window.scrollX, y: window.scrollY };
13+
let scrollUpdateScheduled = false;
1314

1415
// ----
1516
// class Animation
@@ -312,14 +313,22 @@ function getObserver(rootMargin) {
312313
/**
313314
* Scroll infos passed to callbacks, same shape as oneloop.js ScrollObserver (direction.y = 1 when scrolling down).
314315
* Computed on demand: no scroll listener is attached.
316+
* lastScroll is committed on the next animation frame so every caller in the same turn
317+
* (multiple IntersectionObservers, destroy) shares the same delta / direction.
315318
* @return {Object} scroll, delta and direction, matching oneloop ScrollObserver.
316319
*/
317320
function getScrollInfos() {
318321
const scroll = { x: window.scrollX, y: window.scrollY };
319322
const delta = { x: scroll.x - lastScroll.x, y: scroll.y - lastScroll.y };
320323

321-
lastScroll.x = scroll.x;
322-
lastScroll.y = scroll.y;
324+
if (!scrollUpdateScheduled) {
325+
scrollUpdateScheduled = true;
326+
requestAnimationFrame(() => {
327+
lastScroll.x = window.scrollX;
328+
lastScroll.y = window.scrollY;
329+
scrollUpdateScheduled = false;
330+
});
331+
}
323332

324333
return {
325334
scroll,

0 commit comments

Comments
 (0)