2023-01-13 05:40:33 +01:00
|
|
|
type ScrollBehavior = "auto" | "smooth" | "instant";
|
2021-10-09 05:33:08 +02:00
|
|
|
|
2022-06-20 06:20:28 +02:00
|
|
|
export function getScrollContainer(el: HTMLElement | null): HTMLElement | null {
|
2023-01-13 05:40:33 +01:00
|
|
|
if (el == null || el.tagName === "HTML") return null;
|
|
|
|
const overflow = window.getComputedStyle(el).getPropertyValue("overflow-y");
|
|
|
|
if (overflow === "scroll" || overflow === "auto") {
|
2020-02-16 12:58:41 +01:00
|
|
|
return el;
|
|
|
|
} else {
|
|
|
|
return getScrollContainer(el.parentElement);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getScrollPosition(el: Element | null): number {
|
|
|
|
const container = getScrollContainer(el);
|
|
|
|
return container == null ? window.scrollY : container.scrollTop;
|
|
|
|
}
|
|
|
|
|
2020-08-19 10:52:11 +02:00
|
|
|
export function isTopVisible(el: Element | null): boolean {
|
|
|
|
const scrollTop = getScrollPosition(el);
|
|
|
|
const topPosition = el.offsetTop; // TODO: container内でのelの相対位置を取得できればより正確になる
|
|
|
|
|
|
|
|
return scrollTop <= topPosition;
|
|
|
|
}
|
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
export function isBottomVisible(
|
|
|
|
el: HTMLElement,
|
|
|
|
tolerance = 1,
|
|
|
|
container = getScrollContainer(el),
|
|
|
|
) {
|
|
|
|
if (container)
|
|
|
|
return (
|
|
|
|
el.scrollHeight <=
|
|
|
|
container.clientHeight + Math.abs(container.scrollTop) + tolerance
|
|
|
|
);
|
2022-06-20 06:20:28 +02:00
|
|
|
return el.scrollHeight <= window.innerHeight + window.scrollY + tolerance;
|
|
|
|
}
|
|
|
|
|
2020-02-16 12:58:41 +01:00
|
|
|
export function onScrollTop(el: Element, cb) {
|
|
|
|
const container = getScrollContainer(el) || window;
|
2023-01-13 05:40:33 +01:00
|
|
|
const onScroll = (ev) => {
|
2020-02-16 12:58:41 +01:00
|
|
|
if (!document.body.contains(el)) return;
|
2020-08-19 10:52:11 +02:00
|
|
|
if (isTopVisible(el)) {
|
2020-02-16 12:58:41 +01:00
|
|
|
cb();
|
2023-01-13 05:40:33 +01:00
|
|
|
container.removeEventListener("scroll", onScroll);
|
2020-02-16 12:58:41 +01:00
|
|
|
}
|
|
|
|
};
|
2023-01-13 05:40:33 +01:00
|
|
|
container.addEventListener("scroll", onScroll, { passive: true });
|
2020-02-16 12:58:41 +01:00
|
|
|
}
|
2020-07-12 09:14:49 +02:00
|
|
|
|
2020-07-19 05:26:05 +02:00
|
|
|
export function onScrollBottom(el: Element, cb) {
|
|
|
|
const container = getScrollContainer(el) || window;
|
2023-01-13 05:40:33 +01:00
|
|
|
const onScroll = (ev) => {
|
2020-07-19 05:26:05 +02:00
|
|
|
if (!document.body.contains(el)) return;
|
|
|
|
const pos = getScrollPosition(el);
|
|
|
|
if (pos + el.clientHeight > el.scrollHeight - 1) {
|
|
|
|
cb();
|
2023-01-13 05:40:33 +01:00
|
|
|
container.removeEventListener("scroll", onScroll);
|
2020-07-19 05:26:05 +02:00
|
|
|
}
|
|
|
|
};
|
2023-01-13 05:40:33 +01:00
|
|
|
container.addEventListener("scroll", onScroll, { passive: true });
|
2020-07-19 05:26:05 +02:00
|
|
|
}
|
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
export function scroll(
|
|
|
|
el: Element,
|
|
|
|
options: {
|
|
|
|
top?: number;
|
|
|
|
left?: number;
|
|
|
|
behavior?: ScrollBehavior;
|
|
|
|
},
|
|
|
|
) {
|
2020-07-12 09:14:49 +02:00
|
|
|
const container = getScrollContainer(el);
|
|
|
|
if (container == null) {
|
2021-10-09 05:33:08 +02:00
|
|
|
window.scroll(options);
|
2020-07-12 09:14:49 +02:00
|
|
|
} else {
|
2021-10-09 05:33:08 +02:00
|
|
|
container.scroll(options);
|
2020-07-12 09:14:49 +02:00
|
|
|
}
|
|
|
|
}
|
2020-07-19 05:26:05 +02:00
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
export function scrollToTop(
|
|
|
|
el: Element,
|
|
|
|
options: { behavior?: ScrollBehavior } = {},
|
|
|
|
) {
|
2021-10-09 05:33:08 +02:00
|
|
|
scroll(el, { top: 0, ...options });
|
2021-02-14 14:26:07 +01:00
|
|
|
}
|
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
export function scrollToBottom(
|
|
|
|
el: Element,
|
|
|
|
options: { behavior?: ScrollBehavior } = {},
|
|
|
|
) {
|
2021-10-09 05:33:08 +02:00
|
|
|
scroll(el, { top: 99999, ...options }); // TODO: ちゃんと計算する
|
2021-02-14 14:26:07 +01:00
|
|
|
}
|
|
|
|
|
2020-07-19 05:26:05 +02:00
|
|
|
export function isBottom(el: Element, asobi = 0) {
|
|
|
|
const container = getScrollContainer(el);
|
|
|
|
const current = container
|
|
|
|
? el.scrollTop + el.offsetHeight
|
|
|
|
: window.scrollY + window.innerHeight;
|
2023-01-13 05:40:33 +01:00
|
|
|
const max = container ? el.scrollHeight : document.body.offsetHeight;
|
|
|
|
return current >= max - asobi;
|
2020-07-19 05:26:05 +02:00
|
|
|
}
|