UNPKG

@e-group/utils

Version:

eGroup team utils that share across projects.

35 lines (28 loc) 718 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = scrollTo; /* 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; } ; 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(); }