Skip to content

Commit be1fef3

Browse files
committed
add watchScroll option
1 parent 6db90af commit be1fef3

File tree

3 files changed

+30
-12
lines changed

3 files changed

+30
-12
lines changed

demo/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ grid.addEventListener('click', ev => {
3434
});
3535

3636
const { unwrapGrid } = wrapGrid(grid, {
37-
stagger: 20,
3837
easing: 'backOut',
3938
onStart: els => console.log('onstart', els),
4039
onEnd: els => console.log('onend', els),
40+
watchScroll: true,
4141
});
4242

4343
document

src/index.ts

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ export const wrapGrid = (
108108
easing = 'easeInOut',
109109
onStart = () => {},
110110
onEnd = () => {},
111+
watchScroll = false,
111112
}: WrapGridArguments = {},
112113
) => {
113114
if (!popmotionEasing[easing]) {
@@ -140,16 +141,29 @@ export const wrapGrid = (
140141
};
141142
recordPositions(container.children as HTMLCollectionOf<HTMLElement>);
142143

143-
const throttledResizeListener = throttle(() => {
144-
const bodyElement = document.querySelector('body');
145-
const containerIsNoLongerInPage =
146-
bodyElement && !bodyElement.contains(container);
147-
if (!container || containerIsNoLongerInPage) {
148-
window.removeEventListener('resize', throttledResizeListener);
149-
}
150-
recordPositions(container.children as HTMLCollectionOf<HTMLElement>);
151-
}, 250);
152-
window.addEventListener('resize', throttledResizeListener);
144+
const createMutationListener = (
145+
eventType: string,
146+
throttleDuration: number,
147+
) => {
148+
const throttledListener = throttle(() => {
149+
const bodyElement = document.querySelector('body');
150+
const containerIsNoLongerInPage =
151+
bodyElement && !bodyElement.contains(container);
152+
if (!container || containerIsNoLongerInPage) {
153+
window.removeEventListener(eventType, throttledListener);
154+
}
155+
recordPositions(container.children as HTMLCollectionOf<HTMLElement>);
156+
}, throttleDuration);
157+
return throttledListener;
158+
};
159+
160+
const resizeListener = createMutationListener('resize', 250);
161+
window.addEventListener('resize', resizeListener);
162+
163+
const scrollListener = createMutationListener('scroll', 20);
164+
if (watchScroll) {
165+
window.addEventListener('scroll', scrollListener);
166+
}
153167

154168
const mutationCallback = (
155169
mutationsList: MutationRecord[] | 'forceGridAnimation',
@@ -321,7 +335,10 @@ export const wrapGrid = (
321335
attributeFilter: ['class'],
322336
});
323337
const unwrapGrid = () => {
324-
window.removeEventListener('resize', throttledResizeListener);
338+
window.removeEventListener('resize', resizeListener);
339+
if (watchScroll) {
340+
window.removeEventListener('scroll', scrollListener);
341+
}
325342
observer.disconnect();
326343
};
327344
const forceGridAnimation = () => mutationCallback('forceGridAnimation');

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,5 @@ export interface WrapGridArguments {
5252
easing?: keyof PopmotionEasing;
5353
onStart?: (animatedChildren: HTMLElement[]) => void;
5454
onEnd?: (animatedChildren: HTMLElement[]) => void;
55+
watchScroll?: boolean;
5556
}

0 commit comments

Comments
 (0)