From 6c0a197c83295b1256ab8dadf36603f1d64d9b0f Mon Sep 17 00:00:00 2001 From: Serraniel Date: Mon, 8 Feb 2021 19:36:48 +0100 Subject: [PATCH] #161 Better scroll behaviour --- .../enhancements/watch2getherAutoscroll.ts | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/src/javascript/enhancements/watch2getherAutoscroll.ts b/src/javascript/enhancements/watch2getherAutoscroll.ts index 0f0ba36..911ced8 100644 --- a/src/javascript/enhancements/watch2getherAutoscroll.ts +++ b/src/javascript/enhancements/watch2getherAutoscroll.ts @@ -17,12 +17,14 @@ export function init(): void { function observeSearchResults(searchRes: Element): void { let observer = new MutationObserver(mutations => { - mutations.forEach(mutation => { - let scrollTarget = searchRes.querySelector('.ep-view md-list-item:not(.animelist-completed)'); - if (assigned(scrollTarget)) { - scrollTarget.scrollIntoView(); - } - }); + let scrollTarget = searchRes.querySelector('.ep-view md-list-item:not(.animelist-completed)') as HTMLElement; + if (assigned(scrollTarget)) { + // scroll container to episode first + searchRes.scrollTop = scrollTarget.offsetTop; + + // then scroll page to episode if neccessarry + scrollTarget.scrollIntoView({ behavior: "smooth", block: "nearest" }); + } }); observer.observe(searchRes, { @@ -31,11 +33,6 @@ function observeSearchResults(searchRes: Element): void { }); } -function scrollTo(el: Element, pos: number): void { - let offset: number = 24; - el.scrollTo(0, pos * 72 + offset) -} - function findSearchResults(): Element { let searchResults = document.getElementsByClassName('search-results'); if (searchResults[0] instanceof Element) {