2020-02-16 13:58:41 +02:00
|
|
|
export function getScrollContainer(el: Element | null): Element | null {
|
|
|
|
if (el == null || el.tagName === 'BODY') return null;
|
2020-07-11 04:13:11 +03:00
|
|
|
const overflow = window.getComputedStyle(el).getPropertyValue('overflow');
|
|
|
|
if (overflow.endsWith('auto')) { // xとyを個別に指定している場合、hidden auto みたいな値になる
|
2020-02-16 13:58:41 +02: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 11:52:11 +03:00
|
|
|
export function isTopVisible(el: Element | null): boolean {
|
|
|
|
const scrollTop = getScrollPosition(el);
|
|
|
|
const topPosition = el.offsetTop; // TODO: container内でのelの相対位置を取得できればより正確になる
|
|
|
|
|
|
|
|
return scrollTop <= topPosition;
|
|
|
|
}
|
|
|
|
|
2020-02-16 13:58:41 +02:00
|
|
|
export function onScrollTop(el: Element, cb) {
|
|
|
|
const container = getScrollContainer(el) || window;
|
|
|
|
const onScroll = ev => {
|
|
|
|
if (!document.body.contains(el)) return;
|
2020-08-19 11:52:11 +03:00
|
|
|
if (isTopVisible(el)) {
|
2020-02-16 13:58:41 +02:00
|
|
|
cb();
|
2020-08-19 11:51:31 +03:00
|
|
|
container.removeEventListener('scroll', onScroll);
|
2020-02-16 13:58:41 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
container.addEventListener('scroll', onScroll, { passive: true });
|
|
|
|
}
|
2020-07-12 10:14:49 +03:00
|
|
|
|
2020-07-19 06:26:05 +03:00
|
|
|
export function onScrollBottom(el: Element, cb) {
|
|
|
|
const container = getScrollContainer(el) || window;
|
|
|
|
const onScroll = ev => {
|
|
|
|
if (!document.body.contains(el)) return;
|
|
|
|
const pos = getScrollPosition(el);
|
|
|
|
if (pos + el.clientHeight > el.scrollHeight - 1) {
|
|
|
|
cb();
|
2020-08-19 11:51:31 +03:00
|
|
|
container.removeEventListener('scroll', onScroll);
|
2020-07-19 06:26:05 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
container.addEventListener('scroll', onScroll, { passive: true });
|
|
|
|
}
|
|
|
|
|
2020-07-12 10:14:49 +03:00
|
|
|
export function scroll(el: Element, top: number) {
|
|
|
|
const container = getScrollContainer(el);
|
|
|
|
if (container == null) {
|
|
|
|
window.scroll({ top: top, behavior: 'instant' });
|
|
|
|
} else {
|
|
|
|
container.scrollTop = top;
|
|
|
|
}
|
|
|
|
}
|
2020-07-19 06:26:05 +03:00
|
|
|
|
2021-02-14 15:26:07 +02:00
|
|
|
export function scrollToTop(el: Element) {
|
|
|
|
scroll(el, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function scrollToBottom(el: Element) {
|
|
|
|
scroll(el, 99999); // TODO: ちゃんと計算する
|
|
|
|
}
|
|
|
|
|
2020-07-19 06:26:05 +03:00
|
|
|
export function isBottom(el: Element, asobi = 0) {
|
|
|
|
const container = getScrollContainer(el);
|
|
|
|
const current = container
|
|
|
|
? el.scrollTop + el.offsetHeight
|
|
|
|
: window.scrollY + window.innerHeight;
|
|
|
|
const max = container
|
|
|
|
? el.scrollHeight
|
|
|
|
: document.body.offsetHeight;
|
|
|
|
return current >= (max - asobi);
|
|
|
|
}
|