UNPKG

@e-group/utils

Version:

eGroup team utils that share across projects.

27 lines (23 loc) 623 B
/* eslint-disable no-param-reassign */ function easeInOutQuad(t, b, c, d) { t /= d / 2; if (t < 1) return c / 2 * t * t + b; t--; return -c / 2 * (t * (t - 2) - 1) + b; } ; export default function scrollTo(element, to, duration) { const start = element.scrollTop; const change = to - start; let currentTime = 0; const increment = 20; const animateScroll = () => { currentTime += increment; const val = easeInOutQuad(currentTime, start, change, duration); element.scrollTop = val; if (currentTime < duration) { setTimeout(animateScroll, increment); } }; animateScroll(); }