From 35399fe451c148603408938ac60c3a0023e2f6ba Mon Sep 17 00:00:00 2001 From: syuilo Date: Wed, 19 Aug 2020 17:52:11 +0900 Subject: [PATCH] enhance(client): Better element visible detection --- src/client/scripts/paging.ts | 5 ++--- src/client/scripts/scroll.ts | 10 ++++++++-- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/client/scripts/paging.ts b/src/client/scripts/paging.ts index 006d23875..538615afa 100644 --- a/src/client/scripts/paging.ts +++ b/src/client/scripts/paging.ts @@ -1,5 +1,4 @@ -import Vue from 'vue'; -import { getScrollPosition, onScrollTop } from './scroll'; +import { onScrollTop, isTopVisible } from './scroll'; const SECOND_FETCH_LIMIT = 30; @@ -148,7 +147,7 @@ export default (opts) => ({ }, prepend(item) { - const isTop = this.isBackTop || (document.body.contains(this.$el) && (getScrollPosition(this.$el) === 0)); + const isTop = this.isBackTop || (document.body.contains(this.$el) && isTopVisible(this.$el)); if (isTop) { // Prepend the item diff --git a/src/client/scripts/scroll.ts b/src/client/scripts/scroll.ts index bad31a80f..18c336689 100644 --- a/src/client/scripts/scroll.ts +++ b/src/client/scripts/scroll.ts @@ -13,12 +13,18 @@ export function getScrollPosition(el: Element | null): number { return container == null ? window.scrollY : container.scrollTop; } +export function isTopVisible(el: Element | null): boolean { + const scrollTop = getScrollPosition(el); + const topPosition = el.offsetTop; // TODO: container内でのelの相対位置を取得できればより正確になる + + return scrollTop <= topPosition; +} + 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) { + if (isTopVisible(el)) { cb(); container.removeEventListener('scroll', onScroll); }