enhance(client): Better element visible detection

This commit is contained in:
syuilo 2020-08-19 17:52:11 +09:00
parent c54d5e7040
commit 6870262f8d
2 changed files with 10 additions and 5 deletions

View file

@ -1,5 +1,4 @@
import Vue from 'vue'; import { onScrollTop, isTopVisible } from './scroll';
import { getScrollPosition, onScrollTop } from './scroll';
const SECOND_FETCH_LIMIT = 30; const SECOND_FETCH_LIMIT = 30;
@ -148,7 +147,7 @@ export default (opts) => ({
}, },
prepend(item) { 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) { if (isTop) {
// Prepend the item // Prepend the item

View file

@ -13,12 +13,18 @@ export function getScrollPosition(el: Element | null): number {
return container == null ? window.scrollY : container.scrollTop; 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) { export function onScrollTop(el: Element, cb) {
const container = getScrollContainer(el) || window; const container = getScrollContainer(el) || window;
const onScroll = ev => { const onScroll = ev => {
if (!document.body.contains(el)) return; if (!document.body.contains(el)) return;
const pos = getScrollPosition(el); if (isTopVisible(el)) {
if (pos === 0) {
cb(); cb();
container.removeEventListener('scroll', onScroll); container.removeEventListener('scroll', onScroll);
} }