This commit is contained in:
tamaina 2023-07-17 09:50:34 +00:00
parent f565e0f8a5
commit 78944bf441
1 changed files with 4 additions and 24 deletions

View File

@ -233,43 +233,23 @@ watch([$$(weakBacked), $$(contentEl)], () => {
})(); })();
}); });
function preventDefault(ev: Event) {
ev.preventDefault();
}
/** /**
* アイテムを上に追加した場合に追加分だけスクロールを下にずらす * アイテムを上に追加した場合に追加分だけスクロールを下にずらす
* @param fn DOM操作(unshiftItemsなどで) * @param fn DOM操作(unshiftItemsなどで)
*/ */
function adjustScroll(fn: () => void): Promise<void> { function adjustScroll(fn: () => void): Promise<void> {
const oldHeight = scrollableElement ? scrollableElement.scrollHeight : getBodyScrollHeight(); const oldHeight = scrollableElement ? scrollableElement.scrollHeight : getBodyScrollHeight();
const oldScroll = scrollableElement ? scrollableElement.scrollTop : window.scrollY;
//
try {
// scrollableElementOrHtmlundefined
scrollableElementOrHtml.addEventListener('mousewheel', preventDefault, { passive: false });
scrollableElementOrHtml.addEventListener('touchmove', preventDefault, { passive: false });
// try
scroll(scrollableElement, { top: oldScroll, behavior: 'instant' });
} catch (err) {
console.error(err, { scrollableElementOrHtml });
}
denyMoveTransition.value = true; denyMoveTransition.value = true;
fn(); fn();
return nextTick(() => { return nextTick(() => {
try { const diff = (scrollableElement ? scrollableElement.scrollHeight : getBodyScrollHeight()) - oldHeight;
const top = oldScroll + ((scrollableElement ? scrollableElement.scrollHeight : getBodyScrollHeight()) - oldHeight); const newScroll = scrollableElement ? scrollableElement.scrollTop : window.scrollY;
const top = newScroll + diff;
scroll(scrollableElement, { top, behavior: 'instant' }); scroll(scrollableElement, { top, behavior: 'instant' });
// scrollableElementOrHtmlundefined
scrollableElementOrHtml.removeEventListener('mousewheel', preventDefault);
scrollableElementOrHtml.removeEventListener('touchmove', preventDefault);
} catch (err) {
console.error(err, { scrollableElementOrHtml });
}
denyMoveTransition.value = false; denyMoveTransition.value = false;
return nextTick(); return nextTick();
}); });