diff --git a/routes/_actions/scroll.js b/routes/_actions/scroll.js new file mode 100644 index 0000000..4ab36a7 --- /dev/null +++ b/routes/_actions/scroll.js @@ -0,0 +1,30 @@ +const easingOutQuint = (x, t, b, c, d) => + c * ((t = t / d - 1) * t * t * t * t + 1) + b + +const scroll = (node, key, target) => { + const startTime = Date.now() + const offset = node[key] + const gap = target - offset + const duration = 1000 + let interrupt = false + + const step = () => { + const elapsed = Date.now() - startTime + const percentage = elapsed / duration + + if (percentage > 1 || interrupt) { + return + } + + node[key] = easingOutQuint(0, elapsed, offset, gap, duration) + requestAnimationFrame(step) + } + + step() + + return () => { + interrupt = true + } +} + +export const scrollTop = node => scroll(node, 'scrollTop', 0) diff --git a/routes/_components/Nav.html b/routes/_components/Nav.html index ad574b2..0afadeb 100644 --- a/routes/_components/Nav.html +++ b/routes/_components/Nav.html @@ -1,25 +1,25 @@