tdesign-react
Version:
TDesign Component for React
69 lines (65 loc) • 2.08 kB
JavaScript
/**
* tdesign v1.11.6
* (c) 2025 tdesign
* @license MIT
*/
import raf from 'raf';
import { easeInOutCubic } from '../../_util/easing.js';
function getOffsetTop(target, container) {
var _target$getBoundingCl = target.getBoundingClientRect(),
top = _target$getBoundingCl.top;
if (container === window) {
return top - document.documentElement.clientTop;
}
return top - container.getBoundingClientRect().top;
}
function isWindow(obj) {
return obj && obj === obj.window;
}
function getScroll(target, isLeft) {
if (typeof window === "undefined" || !target) {
return 0;
}
var method = isLeft ? "scrollLeft" : "scrollTop";
var result = 0;
if (isWindow(target)) {
result = target[isLeft ? "pageXOffset" : "pageYOffset"];
} else if (target instanceof Document) {
result = target.documentElement[method];
} else if (target) {
result = target[method];
}
return result;
}
function scrollTo(target, opt) {
var _opt$container = opt.container,
container = _opt$container === void 0 ? window : _opt$container,
_opt$duration = opt.duration,
duration = _opt$duration === void 0 ? 450 : _opt$duration,
_opt$easing = opt.easing,
easing = _opt$easing === void 0 ? easeInOutCubic : _opt$easing;
var scrollTop = getScroll(container);
var startTime = Date.now();
return new Promise(function (res) {
var _fnc = function fnc() {
var timestamp = Date.now();
var time = timestamp - startTime;
var nextScrollTop = easing(Math.min(time, duration), scrollTop, target, 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(_fnc);
} else {
raf(res);
}
};
raf(_fnc);
});
}
export { getOffsetTop, getScroll, scrollTo };
//# sourceMappingURL=dom.js.map