@opensig/opendesign
Version:
<p align="center"><img src="../docs/public/opendesign-logo-light.png"/></p> <h1 align="center">opendesign</h1> <p align="center">一个 Vue 3 组件库</p> <p align="center"><b>皮肤可定制,使用 TypeScript</b></p>
147 lines (146 loc) • 4.24 kB
JavaScript
;
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const easing = require("./easing.js");
const helper = require("./helper.js");
const is = require("./is.js");
function isDocument(val) {
return val instanceof Document || (val == null ? void 0 : val.constructor.name) === "HTMLDocument";
}
function isHtmlElement(el) {
if (typeof HTMLElement === "object") {
return el instanceof HTMLElement;
} else if (el && typeof el === "object") {
const ele = el;
return (ele.nodeType === 1 || ele.nodeType === 9) && typeof ele.nodeName === "string";
}
return false;
}
function getOffsetElement(el) {
const offsetEl = el.offsetParent;
if (offsetEl && offsetEl.tagName === "BODY") {
const stylePosition = window.getComputedStyle(document.body).getPropertyValue("position");
if (stylePosition === "static") {
return document.documentElement;
}
}
return offsetEl;
}
function getScroll(el) {
const rlt = {
scrollLeft: 0,
scrollTop: 0
};
if (!el) {
return rlt;
}
if (is.isWindow(el)) {
rlt.scrollLeft = window.scrollX;
rlt.scrollTop = window.scrollY;
} else if (isDocument(el)) {
rlt.scrollLeft = el.documentElement.scrollLeft;
rlt.scrollTop = el.documentElement.scrollTop;
} else {
rlt.scrollLeft = el.scrollLeft;
rlt.scrollTop = el.scrollTop;
}
return rlt;
}
function getScrollParents(el) {
const parents = [];
let ele = el;
while (ele && ele !== document.documentElement) {
const { offsetHeight, offsetWidth, scrollHeight, scrollWidth } = ele;
if (offsetHeight < scrollHeight || offsetWidth < scrollWidth) {
parents.push(ele);
}
ele = ele.parentElement;
}
return parents;
}
function getElementSize(el) {
return {
width: el.innerWidth || el.clientWidth,
height: el.innerHeight || el.clientHeight,
offsetWidth: el.innerWidth || el.offsetWidth,
offsetHeight: el.innerHeight || el.offsetHeight
};
}
function getElementBorder(el, dir) {
const style = window.getComputedStyle(el);
let d = [];
{
d = is.isArray(dir) ? dir : ["left", "right", "bottom", "top"];
}
const rlt = {};
d.forEach((k) => {
rlt[k] = parseFloat(style.getPropertyValue(`border-${k}-width`));
});
return rlt;
}
function supportTouch() {
return "ontouchstart" in window;
}
function mergeClass(...classList) {
let rlt = [];
classList.forEach((item) => {
if (is.isArray(item)) {
rlt = rlt.concat(item);
} else {
rlt.push(item);
}
});
return rlt;
}
let cancelScrollRAF = null;
function scrollTo(y, opts) {
const { container = window, duration = 450 } = opts;
const { scrollTop } = getScroll(container);
const startTime = Date.now();
if (is.isFunction(cancelScrollRAF)) {
cancelScrollRAF();
cancelScrollRAF = null;
}
return new Promise((resolve) => {
const frameFn = () => {
const timeStamp = Date.now();
const time = timeStamp - startTime;
const nextScrollTop = easing.easeInOutCubic(time > duration ? duration : time, scrollTop, y, duration);
if (is.isWindow(container)) {
window.scrollTo({
left: window.scrollX,
top: nextScrollTop,
behavior: "instant"
});
} else if (isDocument(container)) {
container.documentElement.scrollTop = nextScrollTop;
} else {
container.scrollTop = nextScrollTop;
}
if (time < duration) {
const fn = helper.throttleRAF(frameFn);
cancelScrollRAF = fn.cancel;
fn();
} else {
helper.throttleRAF(resolve)();
}
};
helper.throttleRAF(frameFn)();
});
}
function isOverflown(element) {
if (!element) {
return false;
}
return element.scrollWidth > element.clientWidth || element.scrollHeight > element.clientHeight;
}
exports.getElementBorder = getElementBorder;
exports.getElementSize = getElementSize;
exports.getOffsetElement = getOffsetElement;
exports.getScroll = getScroll;
exports.getScrollParents = getScrollParents;
exports.isDocument = isDocument;
exports.isHtmlElement = isHtmlElement;
exports.isOverflown = isOverflown;
exports.mergeClass = mergeClass;
exports.scrollTo = scrollTo;
exports.supportTouch = supportTouch;