12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- import { raf, cancelRaf } from '../utils/dom/raf';
- import { getScrollTop, setScrollTop } from '../utils/dom/scroll';
- var scrollLeftRafId;
- export function scrollLeftTo(scroller, to, duration) {
- cancelRaf(scrollLeftRafId);
- var count = 0;
- var from = scroller.scrollLeft;
- var frames = duration === 0 ? 1 : Math.round(duration * 1000 / 16);
- function animate() {
- scroller.scrollLeft += (to - from) / frames;
- if (++count < frames) {
- scrollLeftRafId = raf(animate);
- }
- }
- animate();
- }
- export function scrollTopTo(scroller, to, duration, callback) {
- var current = getScrollTop(scroller);
- var isDown = current < to;
- var frames = duration === 0 ? 1 : Math.round(duration * 1000 / 16);
- var step = (to - current) / frames;
- function animate() {
- current += step;
- if (isDown && current > to || !isDown && current < to) {
- current = to;
- }
- setScrollTop(scroller, current);
- if (isDown && current < to || !isDown && current > to) {
- raf(animate);
- } else if (callback) {
- raf(callback);
- }
- }
- animate();
- }
|