UNPKG

691 BJavaScriptView Raw
1import * as FUNCTIONS from './snap-functions';
2
3function snap($el, to, scroll, { delay = 10, duration = 75, fn = 'easeOutCubic', step = 5 }) {
4 let currentTime = 0;
5 const next = FUNCTIONS[fn] || FUNCTIONS.easeOutCubic;
6 const start = $el[scroll];
7 const change = to - start;
8
9 const animate = () => {
10 currentTime += step;
11
12 $el[scroll] = next(currentTime, start, change, duration);
13
14 if (currentTime < duration) {
15 requestAnimationFrame(animate);
16 }
17 };
18
19 setTimeout(animate, delay);
20}
21
22export function snapY($el, to, options = {}) {
23 snap($el, to, 'scrollTop', options);
24}
25
26export function snapX($el, to, options = {}) {
27 snap($el, to, 'scrollLeft', options);
28}