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();
}
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({

View File

@ -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();
}
};