@opensig/opendesign
Version:
135 lines (134 loc) • 5.47 kB
TypeScript
import { PositionT } from './types';
export type ScrollTarget = HTMLElement | Window | Document;
export declare function isDocument(val: unknown): val is Document;
export declare function isHtmlElement(el: unknown): el is HTMLElement;
export declare function getOffsetElement(el: HTMLElement): Element | null;
export declare function getScroll(el: ScrollTarget): {
scrollLeft: number;
scrollTop: number;
};
/**
* 获取元素的所有可滚动的父元素
* @param el - 起始 HTML 元素
* @returns 从当前元素向上遍历找到的所有可滚动父元素数组(不包含 document.documentElement)
*/
export declare function getScrollParents(el: HTMLElement): HTMLElement[];
/**
* 从触发事件的目标元素向上遍历 DOM 树,查找第一个包含指定类名的元素
* @param target - 事件触发的原始 DOM 元素(e.target)
* @param className - 要查找的目标类名(纯类名字符串,无需带 .)
* @param rootContainer - 遍历的根边界容器(遍历到该容器则停止,不再向上查找)
* @returns 找到的带指定类名的元素 | 未找到则返回 null
* @example
* // 假设父容器是 #parent-container,点击了目标元素的子span
* const parent = document.getElementById('parent-container');
* const target = findClosestElementWithClass(e.target, 'target-item', parent);
* if (target) { console.log('找到目标元素:', target); }
*/
export declare function findClosestElementWithClass(target: EventTarget | null, className: string, rootContainer: HTMLElement): HTMLElement | null;
export declare function getRelativeBounding(e: DOMRect, c: DOMRect): {
top: number;
bottom: number;
left: number;
right: number;
width: number;
height: number;
offsetLeft: number;
offsetTop: number;
offsetRight: number;
offsetBottom: number;
};
export type RelativeRect = ReturnType<typeof getRelativeBounding>;
export declare function getElementSize(el: HTMLElement | Window): {
width: number;
height: number;
offsetWidth: number;
offsetHeight: number;
};
/**
* 用requestAnimationFrame确保布局完成后获取元素尺寸
* IOS下table内的元素不会马上渲染给出高度
*/
export declare function getElementRectByRAF(el: HTMLElement): Promise<DOMRect>;
export declare function getElementBorder(el: HTMLElement, dir?: PositionT | PositionT[]): {
left?: number;
right?: number;
top?: number;
bottom?: number;
};
export declare function getCssVariable(key: string, el?: HTMLElement): string;
export declare function supportTouch(): boolean;
interface ScrollTopOptions {
container?: ScrollTarget;
duration?: number;
}
export declare function scrollTo(y: number, opts: ScrollTopOptions): Promise<unknown>;
export declare function isOverflown(element?: HTMLElement): boolean;
/**
* 判断元素是否在视口内
*/
export declare function isInViewport(element?: Element): boolean;
/**
* 判断是否为不可见标签
*/
export declare function isNonVisibleTag(element: Element): boolean;
/**
* 判断元素是否在视觉上隐藏
*/
export declare function isElementHidden(element: HTMLElement): boolean;
/**
* 判断元素左右边界是否超出滚动父元素的可视区域,并计算超出的像素值
* @param {HTMLElement} options.element - 目标元素
* @param {HTMLElement} options.parentElement - 目标父元素, 可选
* @param {number} options.threshold - 阈值,可选,如果溢出值大于阈值才算溢出
* @returns 包含是否超出、超出左侧/右侧像素值的结果
*/
export declare function checkElementOverflowHorizontal(options: {
element: HTMLElement;
parentElement?: HTMLElement;
threshold?: number;
}): {
isOverflowLeft: boolean;
isOverflowRight: boolean;
overflowLeft: number;
overflowRight: number;
};
/**
* 判断元素上下边界是否超出滚动父元素的可视区域,并计算超出的像素值
* @param {HTMLElement} options.element - 目标元素
* @param {HTMLElement} options.parentElement - 目标父元素, 可选
* @param {number} options.threshold - 阈值,可选,如果溢出值大于阈值才算溢出
* @returns 包含是否超出、超出上下像素值的结果
*/
export declare function checkElementOverflowVertical(options: {
element: HTMLElement;
parentElement?: HTMLElement;
threshold?: number;
}): {
isOverflowTop: boolean;
isOverflowBottom: boolean;
overflowTop: number;
overflowBottom: number;
};
/**
* 判断元素四个边界是否超出滚动父容器的可视区域,并计算各方向超出的像素值。与 isOverflown 不同,后者检测的是元素自身内容是否超出自身盒子边界
* @param {HTMLElement} options.element - 目标元素
* @param {HTMLElement} options.parentElement - 目标父元素,可选,不传则取最近的滚动父元素
* @param {number} options.threshold - 阈值,可选,超出值大于阈值才算溢出(可用于规避浮点误差)
* @returns 包含是否超出、各方向超出像素值的结果
*/
export declare function checkElementOverflow(options: {
element: HTMLElement;
parentElement?: HTMLElement;
threshold?: number;
}): {
isOverflowTop: boolean;
isOverflowBottom: boolean;
overflowTop: number;
overflowBottom: number;
isOverflowLeft: boolean;
isOverflowRight: boolean;
overflowLeft: number;
overflowRight: number;
};
export {};