diff options
author | Bob Zeleznik <zzzman@gmail.com> | 2019-09-28 23:43:25 -0400 |
---|---|---|
committer | Bob Zeleznik <zzzman@gmail.com> | 2019-09-28 23:43:25 -0400 |
commit | a7fc637fb8a9ecf52e737a1d0e28b3805193b82e (patch) | |
tree | 850e58795a927982809f9807a928c95ed59a817b /src/Utils.ts | |
parent | a80f0867032a4735b319c87c1c7c045f062a7d4f (diff) |
lots of fixes to pdfs and link following.
Diffstat (limited to 'src/Utils.ts')
-rw-r--r-- | src/Utils.ts | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/src/Utils.ts b/src/Utils.ts index 4fac53c7d..2b00a6530 100644 --- a/src/Utils.ts +++ b/src/Utils.ts @@ -307,4 +307,34 @@ export function PostToServer(relativeRoute: string, body: any) { body: body }; return requestPromise.post(options); +} + +const easeInOutQuad = (currentTime: number, start: number, change: number, duration: number) => { + let newCurrentTime = currentTime / (duration / 2); + + if (newCurrentTime < 1) { + return (change / 2) * newCurrentTime * newCurrentTime + start; + } + + newCurrentTime -= 1; + return (-change / 2) * (newCurrentTime * (newCurrentTime - 2) - 1) + start; +}; + +export default function smoothScroll(duration: number, element: HTMLElement, to: number) { + const start = element.scrollTop; + const change = to - start; + const startDate = new Date().getTime(); + + const animateScroll = () => { + const currentDate = new Date().getTime(); + const currentTime = currentDate - startDate; + element.scrollTop = easeInOutQuad(currentTime, start, change, duration); + + if (currentTime < duration) { + requestAnimationFrame(animateScroll); + } else { + element.scrollTop = to; + } + }; + animateScroll(); }
\ No newline at end of file |