40 lines
1.5 KiB
JavaScript
40 lines
1.5 KiB
JavaScript
"use strict";
|
|
|
|
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
|
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
Object.defineProperty(exports, "__esModule", {
|
|
value: true
|
|
});
|
|
exports.default = scrollTo;
|
|
var _raf = _interopRequireDefault(require("rc-util/lib/raf"));
|
|
var _easings = require("./easings");
|
|
var _getScroll = _interopRequireWildcard(require("./getScroll"));
|
|
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 = (0, _getScroll.default)(container, true);
|
|
const startTime = Date.now();
|
|
const frameFunc = () => {
|
|
const timestamp = Date.now();
|
|
const time = timestamp - startTime;
|
|
const nextScrollTop = (0, _easings.easeInOutCubic)(time > duration ? duration : time, scrollTop, y, duration);
|
|
if ((0, _getScroll.isWindow)(container)) {
|
|
container.scrollTo(window.pageXOffset, nextScrollTop);
|
|
} else if (container instanceof Document || container.constructor.name === 'HTMLDocument') {
|
|
container.documentElement.scrollTop = nextScrollTop;
|
|
} else {
|
|
container.scrollTop = nextScrollTop;
|
|
}
|
|
if (time < duration) {
|
|
(0, _raf.default)(frameFunc);
|
|
} else if (typeof callback === 'function') {
|
|
callback();
|
|
}
|
|
};
|
|
(0, _raf.default)(frameFunc);
|
|
} |