From 7467aba75e80e40d8625b2372a45be4a590c0858 Mon Sep 17 00:00:00 2001 From: roymondchen Date: Wed, 22 Jun 2022 14:06:37 +0800 Subject: [PATCH] =?UTF-8?q?chore(stage):=20=E4=BF=AE=E6=94=B9=E5=88=A4?= =?UTF-8?q?=E6=96=ADposition=E7=9A=84=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/stage/src/util.ts | 30 +++++++++--------------------- 1 file changed, 9 insertions(+), 21 deletions(-) diff --git a/packages/stage/src/util.ts b/packages/stage/src/util.ts index 9a5ceeaa..7b8b5e64 100644 --- a/packages/stage/src/util.ts +++ b/packages/stage/src/util.ts @@ -73,31 +73,19 @@ export const isSameDomain = (targetUrl = '', source = globalThis.location.host) return getHost(targetUrl) === source; }; -export const isAbsolute = (el?: HTMLElement): boolean => { - if (!el) return false; - return getComputedStyle(el).position === 'absolute'; -}; +export const isAbsolute = (style: CSSStyleDeclaration): boolean => style.position === 'absolute'; -export const isRelative = (el?: HTMLElement): boolean => { - if (!el) return false; - return getComputedStyle(el).position === 'relative'; -}; +export const isRelative = (style: CSSStyleDeclaration): boolean => style.position === 'relative'; -export const isStatic = (el?: HTMLElement): boolean => { - if (!el) return false; - return getComputedStyle(el).position === 'static'; -}; +export const isStatic = (style: CSSStyleDeclaration): boolean => style.position === 'static'; -export const isFixed = (el?: HTMLElement): boolean => { - if (!el) return false; - return getComputedStyle(el).position === 'fixed'; -}; +export const isFixed = (style: CSSStyleDeclaration): boolean => style.position === 'fixed'; export const isFixedParent = (el: HTMLElement) => { let fixed = false; let dom = el; while (dom) { - fixed = isFixed(dom); + fixed = isFixed(getComputedStyle(dom)); if (fixed) { break; } @@ -112,22 +100,22 @@ export const isFixedParent = (el: HTMLElement) => { export const getMode = (el: HTMLElement): Mode => { if (isFixedParent(el)) return Mode.FIXED; - if (isStatic(el) || isRelative(el)) return Mode.SORTABLE; + const style = getComputedStyle(el); + if (isStatic(style) || isRelative(style)) return Mode.SORTABLE; return Mode.ABSOLUTE; }; export const getScrollParent = (element: HTMLElement, includeHidden = false): HTMLElement | null => { let style = getComputedStyle(element); - const excludeStaticParent = style.position === 'absolute'; const overflowRegex = includeHidden ? /(auto|scroll|hidden)/ : /(auto|scroll)/; - if (style.position === 'fixed') return null; + if (isFixed(style)) return null; for (let parent = element; parent.parentElement; ) { parent = parent.parentElement; style = getComputedStyle(parent); - if (excludeStaticParent && style.position === 'static') continue; + if (isAbsolute(style) && isStatic(style)) continue; if (overflowRegex.test(style.overflow + style.overflowY + style.overflowX)) return parent; }