获取子容器在父容器中的上下左右相对位置

27 阅读1分钟

export function getRelativePosition(child, parent) {
	const childRect = child.getBoundingClientRect();
	const parentRect = parent.getBoundingClientRect();

	const top = childRect.top - parentRect.top;
	const right = parentRect.right - childRect.right;
	const bottom = parentRect.bottom - childRect.bottom;
	const left = childRect.left - parentRect.left;

	return { childRect, parentRect, top, right, bottom, left };
}