From 3ae0d91e1f5c2f6790c3bc3f4ad23a5949a53fd3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8F=B5=E4=B9=8B?= Date: Thu, 30 Nov 2023 13:26:33 +0800 Subject: [PATCH] fix: container will be null when at shadowRoot --- src/utils.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/utils.ts b/src/utils.ts index 9ac9b29..1614b42 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -235,13 +235,17 @@ function getOffsetParent(element: HTMLElement): HTMLElement | null { * @returns */ export function getViewPort(container: HTMLElement): HTMLElement { + const fallbackViewportElement = document.documentElement; + + if (!container) { + return fallbackViewportElement; + } + // 若 container 本身就是滚动容器,则直接返回 if (isContentClippedElement(container)) { return container; } - const fallbackViewportElement = document.documentElement; - // 若 container 的 position 是 absolute 或 fixed,则有可能会脱离其最近的滚动容器,需要根据 offsetParent 和 containing block来综合判断 if (['fixed', 'absolute'].includes(getStyle(container, 'position'))) { // 先获取定位节点(若无则使用 containerBlock)