tdesign-react
Version:
TDesign Component for React
102 lines (98 loc) • 3.38 kB
JavaScript
/**
* tdesign v1.15.1
* (c) 2025 tdesign
* @license MIT
*/
import { isString } from 'lodash-es';
import raf from 'raf';
import { e as isWindow } from '../_chunks/dep-3a09424a.js';
import { easeInOutCubic } from './easing.js';
function hasBodyScrollbar() {
return document.body.scrollHeight > document.documentElement.clientHeight;
}
var isNodeOverflow = function isNodeOverflow(ele) {
var _ele$clientWidth = ele.clientWidth,
clientWidth = _ele$clientWidth === void 0 ? 0 : _ele$clientWidth,
_ele$scrollWidth = ele.scrollWidth,
scrollWidth = _ele$scrollWidth === void 0 ? 0 : _ele$scrollWidth;
if (scrollWidth > clientWidth) {
return true;
}
return false;
};
var getScrollContainer = function getScrollContainer() {
var container = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "body";
if (isString(container)) {
return container ? document.querySelector(container) : window;
}
if (typeof container === "function") {
return container();
}
return container || 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 (typeof HTMLDocument !== "undefined" && (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);
});
}
function getWindowScroll() {
var _document = document,
body = _document.body;
var docElm = document.documentElement;
var scrollTop = window.pageYOffset || docElm.scrollTop || body.scrollTop;
var scrollLeft = window.pageXOffset || docElm.scrollLeft || body.scrollLeft;
return {
scrollTop: scrollTop,
scrollLeft: scrollLeft
};
}
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;
}
export { getOffsetTop, getScroll, getScrollContainer, getWindowScroll, hasBodyScrollbar, isNodeOverflow, scrollTo };
//# sourceMappingURL=scroll.js.map