From 7a4d61769946578bb2381e4f3876296a9ddd7974 Mon Sep 17 00:00:00 2001 From: tamaina Date: Fri, 4 Feb 2022 01:42:05 +0900 Subject: [PATCH] scroll event once or not --- packages/client/src/pages/messaging/messaging-room.vue | 6 +++++- packages/client/src/scripts/scroll.ts | 8 ++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/packages/client/src/pages/messaging/messaging-room.vue b/packages/client/src/pages/messaging/messaging-room.vue index 78f6daaa5..48401db79 100644 --- a/packages/client/src/pages/messaging/messaging-room.vue +++ b/packages/client/src/pages/messaging/messaging-room.vue @@ -251,11 +251,14 @@ function onIndicatorClick() { thisScrollToBottom(); } +let scrollRemove: (() => void) | null = $ref(null); + function notifyNewMessage() { showIndicator = true; - onScrollBottom(rootEl, () => { + scrollRemove = onScrollBottom(rootEl, () => { showIndicator = false; + scrollRemove = null; }); } @@ -277,6 +280,7 @@ onMounted(() => { onBeforeUnmount(() => { connection?.dispose(); document.removeEventListener('visibilitychange', onVisibilitychange); + if (scrollRemove) scrollRemove(); }); defineExpose({ diff --git a/packages/client/src/scripts/scroll.ts b/packages/client/src/scripts/scroll.ts index 11a3fa440..b8dadc35a 100644 --- a/packages/client/src/scripts/scroll.ts +++ b/packages/client/src/scripts/scroll.ts @@ -25,7 +25,7 @@ export function getScrollPosition(el: HTMLElement | null): number { return container == null ? window.scrollY : container.scrollTop; } -export function onScrollTop(el: HTMLElement, cb: Function, asobi: number = 1) { +export function onScrollTop(el: HTMLElement, cb: Function, asobi: number = 1, once: boolean = false) { // とりあえず評価してみる if (isTopVisible(el)) { cb(); @@ -38,7 +38,7 @@ export function onScrollTop(el: HTMLElement, cb: Function, asobi: number = 1) { if (!document.body.contains(el)) return; if (isTopVisible(el, asobi)) { cb(); - removeListener(); + if (once) removeListener(); } }; @@ -47,7 +47,7 @@ export function onScrollTop(el: HTMLElement, cb: Function, asobi: number = 1) { return removeListener; } -export function onScrollBottom(el: HTMLElement, cb: Function, asobi: number = 1) { +export function onScrollBottom(el: HTMLElement, cb: Function, asobi: number = 1, once: boolean = false) { const container = getScrollContainer(el); // とりあえず評価してみる @@ -61,7 +61,7 @@ export function onScrollBottom(el: HTMLElement, cb: Function, asobi: number = 1) if (!document.body.contains(el)) return; if (isBottomVisible(el, 1, container)) { cb(); - removeListener(); + if (once) removeListener(); } };