diff options
| author | syuilo <Syuilotan@yahoo.co.jp> | 2020-02-16 20:58:41 +0900 |
|---|---|---|
| committer | syuilo <Syuilotan@yahoo.co.jp> | 2020-02-16 20:58:41 +0900 |
| commit | a5b92e316c197b1dd4422b77525601fc014b1f13 (patch) | |
| tree | ecf9cf4641ca7f3492fd0b1014279aadc698fbc6 /src/client/scripts/scroll.ts | |
| parent | Merge branch 'develop' of https://github.com/syuilo/misskey into develop (diff) | |
| download | sharkey-a5b92e316c197b1dd4422b77525601fc014b1f13.tar.gz sharkey-a5b92e316c197b1dd4422b77525601fc014b1f13.tar.bz2 sharkey-a5b92e316c197b1dd4422b77525601fc014b1f13.zip | |
Refactor: Extract scroll utility functions
Diffstat (limited to 'src/client/scripts/scroll.ts')
| -rw-r--r-- | src/client/scripts/scroll.ts | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/src/client/scripts/scroll.ts b/src/client/scripts/scroll.ts new file mode 100644 index 0000000000..76881bbde1 --- /dev/null +++ b/src/client/scripts/scroll.ts @@ -0,0 +1,27 @@ +export function getScrollContainer(el: Element | null): Element | null { + if (el == null || el.tagName === 'BODY') return null; + const style = window.getComputedStyle(el); + if (style.getPropertyValue('overflow') === 'auto') { + 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; +} + +export function onScrollTop(el: Element, cb) { + const container = getScrollContainer(el) || window; + const onScroll = ev => { + if (!document.body.contains(el)) return; + const pos = getScrollPosition(el); + if (pos === 0) { + cb(); + container.removeEventListener('scroll', onscroll); + } + }; + container.addEventListener('scroll', onScroll, { passive: true }); +} |