diff --git a/packages/runtime-dom/src/components/TransitionGroup.ts b/packages/runtime-dom/src/components/TransitionGroup.ts index 7bb5ce0c6b1..202071f5c1c 100644 --- a/packages/runtime-dom/src/components/TransitionGroup.ts +++ b/packages/runtime-dom/src/components/TransitionGroup.ts @@ -29,8 +29,13 @@ import { } from '@vue/runtime-core' import { extend } from '@vue/shared' -const positionMap = new WeakMap() -const newPositionMap = new WeakMap() +interface Position { + left: number + top: number +} + +const positionMap = new WeakMap() +const newPositionMap = new WeakMap() const moveCbKey = Symbol('_moveCb') const enterCbKey = Symbol('_enterCb') @@ -145,10 +150,7 @@ const TransitionGroupImpl: ComponentOptions = /*@__PURE__*/ decorate({ instance, ), ) - positionMap.set( - child, - (child.el as Element).getBoundingClientRect(), - ) + positionMap.set(child, getRelativePosition(child.el as Element)) } } } @@ -188,8 +190,24 @@ function callPendingCbs(c: VNode) { } } +function getRelativePosition(el: Element): Position { + const elRect = el.getBoundingClientRect() + if (!el.parentElement) { + return { + left: elRect.left, + top: elRect.top, + } + } + + const parentRect = el.parentElement.getBoundingClientRect() + return { + left: elRect.left - parentRect.left, + top: elRect.top - parentRect.top, + } +} + function recordPosition(c: VNode) { - newPositionMap.set(c, (c.el as Element).getBoundingClientRect()) + newPositionMap.set(c, getRelativePosition(c.el as Element)) } function applyTranslation(c: VNode): VNode | undefined {