import { raf } from '../utils/dom/raf';
|
import { getScrollTop, setScrollTop } from '../utils/dom/scroll';
|
export function scrollLeftTo(scroller, to, duration) {
|
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) {
|
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();
|
}
|