scroll event once or not

This commit is contained in:
tamaina 2022-02-04 01:42:05 +09:00
parent b96e98b0f1
commit 7a4d617699
2 changed files with 9 additions and 5 deletions

View File

@ -251,11 +251,14 @@ function onIndicatorClick() {
thisScrollToBottom(); thisScrollToBottom();
} }
let scrollRemove: (() => void) | null = $ref(null);
function notifyNewMessage() { function notifyNewMessage() {
showIndicator = true; showIndicator = true;
onScrollBottom(rootEl, () => { scrollRemove = onScrollBottom(rootEl, () => {
showIndicator = false; showIndicator = false;
scrollRemove = null;
}); });
} }
@ -277,6 +280,7 @@ onMounted(() => {
onBeforeUnmount(() => { onBeforeUnmount(() => {
connection?.dispose(); connection?.dispose();
document.removeEventListener('visibilitychange', onVisibilitychange); document.removeEventListener('visibilitychange', onVisibilitychange);
if (scrollRemove) scrollRemove();
}); });
defineExpose({ defineExpose({

View File

@ -25,7 +25,7 @@ export function getScrollPosition(el: HTMLElement | null): number {
return container == null ? window.scrollY : container.scrollTop; 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)) { if (isTopVisible(el)) {
cb(); cb();
@ -38,7 +38,7 @@ export function onScrollTop(el: HTMLElement, cb: Function, asobi: number = 1) {
if (!document.body.contains(el)) return; if (!document.body.contains(el)) return;
if (isTopVisible(el, asobi)) { if (isTopVisible(el, asobi)) {
cb(); cb();
removeListener(); if (once) removeListener();
} }
}; };
@ -47,7 +47,7 @@ export function onScrollTop(el: HTMLElement, cb: Function, asobi: number = 1) {
return removeListener; 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); 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 (!document.body.contains(el)) return;
if (isBottomVisible(el, 1, container)) { if (isBottomVisible(el, 1, container)) {
cb(); cb();
removeListener(); if (once) removeListener();
} }
}; };