| 1234567891011121314151617181920212223242526272829303132 |
- import raf from './raf';
- import { easeInOutCubic } from './easings';
- import getScroll, { isWindow } from './getScroll';
- export default function scrollTo(y) {
- let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
- const {
- getContainer = () => window,
- callback,
- duration = 450
- } = options;
- const container = getContainer();
- const scrollTop = getScroll(container, true);
- const startTime = Date.now();
- const frameFunc = () => {
- const timestamp = Date.now();
- const time = timestamp - startTime;
- const nextScrollTop = easeInOutCubic(time > duration ? duration : time, scrollTop, y, duration);
- if (isWindow(container)) {
- container.scrollTo(window.scrollX, nextScrollTop);
- } else if (container instanceof Document) {
- container.documentElement.scrollTop = nextScrollTop;
- } else {
- container.scrollTop = nextScrollTop;
- }
- if (time < duration) {
- raf(frameFunc);
- } else if (typeof callback === 'function') {
- callback();
- }
- };
- raf(frameFunc);
- }
|