import {iElementRect} from "./type.base"; /** * 得到元素的位置信息 * @author 韦胜健 * @date 2022/4/3 20:12 */ export function getElementRect(el: HTMLElement): iElementRect { const { clientHeight, clientWidth } = document.documentElement; // @ts-ignore if (el === window) {el = document.documentElement;} if (el === document.documentElement) { return { top: 0, left: 0, right: 0, bottom: 0, height: clientHeight, width: clientWidth }; } else { const { top, left, width, height } = el.getBoundingClientRect()!; const { offsetWidth, offsetHeight } = el; return { top, left, /*当body的高度不够盛满documentElement的时候,right以及bottom的值不可信。这里用总的页面宽高计算right以及bottom*/ right: clientWidth - left - width, bottom: clientHeight - top - height, /*svg元素的 offsetWidth,offsetHeight 为undefined,而内联元素的 clientWidth以及clientHeight为0*/ width: offsetWidth || Math.ceil(width), height: offsetHeight || Math.ceil(height), }; } }