yantd
Version:
React component library
32 lines (31 loc) • 1.29 kB
JavaScript
import raf from 'rc-util/lib/raf';
import getScroll, { isWindow } from './getScroll';
import { easeInOutCubic } from './easings';
export default function scrollTo(y, options) {
if (options === void 0) { options = {}; }
var _a = options.getContainer, getContainer = _a === void 0 ? function () { return window; } : _a, callback = options.callback, _b = options.duration, duration = _b === void 0 ? 450 : _b;
var container = getContainer();
var scrollTop = getScroll(container, true);
var startTime = Date.now();
var frameFunc = function () {
var timestamp = Date.now();
var time = timestamp - startTime;
var nextScrollTop = easeInOutCubic(time > duration ? duration : time, scrollTop, y, duration);
if (isWindow(container)) {
container.scrollTo(window.pageXOffset, nextScrollTop);
}
else if (container instanceof HTMLDocument || container.constructor.name === 'HTMLDocument') {
container.documentElement.scrollTop = nextScrollTop;
}
else {
container.scrollTop = nextScrollTop;
}
if (time < duration) {
raf(frameFunc);
}
else if (typeof callback === 'function') {
callback();
}
};
raf(frameFunc);
}