scrollTo.js
1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
"use strict";
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = scrollTo;
var _raf = _interopRequireDefault(require("rc-util/lib/raf"));
var _getScroll = _interopRequireWildcard(require("./getScroll"));
var _easings = require("./easings");
function scrollTo(y) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var _options$getContainer = options.getContainer,
getContainer = _options$getContainer === void 0 ? function () {
return window;
} : _options$getContainer,
callback = options.callback,
_options$duration = options.duration,
duration = _options$duration === void 0 ? 450 : _options$duration;
var container = getContainer();
var scrollTop = (0, _getScroll["default"])(container, true);
var startTime = Date.now();
var frameFunc = function frameFunc() {
var timestamp = Date.now();
var time = timestamp - startTime;
var 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 HTMLDocument || 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);
}