perf(affix): reduce unnecessary calculations (#6509)

pull/6516/head
Simon He 2023-04-28 14:04:35 +08:00 committed by GitHub
parent ccf850552d
commit 019b539f16
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 26 additions and 9 deletions

View File

@ -109,31 +109,48 @@ const Affix = defineComponent({
const newState = { const newState = {
status: AffixStatus.None, status: AffixStatus.None,
} as AffixState; } as AffixState;
const targetRect = getTargetRect(targetNode);
const placeholderRect = getTargetRect(placeholderNode.value as HTMLElement); const placeholderRect = getTargetRect(placeholderNode.value as HTMLElement);
if (
placeholderRect.top === 0 &&
placeholderRect.left === 0 &&
placeholderRect.width === 0 &&
placeholderRect.height === 0
) {
return;
}
const targetRect = getTargetRect(targetNode);
const fixedTop = getFixedTop(placeholderRect, targetRect, offsetTop.value); const fixedTop = getFixedTop(placeholderRect, targetRect, offsetTop.value);
const fixedBottom = getFixedBottom(placeholderRect, targetRect, offsetBottom.value); const fixedBottom = getFixedBottom(placeholderRect, targetRect, offsetBottom.value);
if (fixedTop !== undefined) { if (fixedTop !== undefined) {
const width = `${placeholderRect.width}px`;
const height = `${placeholderRect.height}px`;
newState.affixStyle = { newState.affixStyle = {
position: 'fixed', position: 'fixed',
top: fixedTop, top: fixedTop,
width: placeholderRect.width + 'px', width,
height: placeholderRect.height + 'px', height,
}; };
newState.placeholderStyle = { newState.placeholderStyle = {
width: placeholderRect.width + 'px', width,
height: placeholderRect.height + 'px', height,
}; };
} else if (fixedBottom !== undefined) { } else if (fixedBottom !== undefined) {
const width = `${placeholderRect.width}px`;
const height = `${placeholderRect.height}px`;
newState.affixStyle = { newState.affixStyle = {
position: 'fixed', position: 'fixed',
bottom: fixedBottom, bottom: fixedBottom,
width: placeholderRect.width + 'px', width,
height: placeholderRect.height + 'px', height,
}; };
newState.placeholderStyle = { newState.placeholderStyle = {
width: placeholderRect.width + 'px', width,
height: placeholderRect.height + 'px', height,
}; };
} }