Skip to content

Commit 019b539

Browse files
authored
perf(affix): reduce unnecessary calculations (#6509)
1 parent ccf8505 commit 019b539

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

components/affix/index.tsx

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -109,31 +109,48 @@ const Affix = defineComponent({
109109
const newState = {
110110
status: AffixStatus.None,
111111
} as AffixState;
112-
const targetRect = getTargetRect(targetNode);
113112
const placeholderRect = getTargetRect(placeholderNode.value as HTMLElement);
113+
114+
if (
115+
placeholderRect.top === 0 &&
116+
placeholderRect.left === 0 &&
117+
placeholderRect.width === 0 &&
118+
placeholderRect.height === 0
119+
) {
120+
return;
121+
}
122+
123+
const targetRect = getTargetRect(targetNode);
114124
const fixedTop = getFixedTop(placeholderRect, targetRect, offsetTop.value);
115125
const fixedBottom = getFixedBottom(placeholderRect, targetRect, offsetBottom.value);
126+
116127
if (fixedTop !== undefined) {
128+
const width = `${placeholderRect.width}px`;
129+
const height = `${placeholderRect.height}px`;
130+
117131
newState.affixStyle = {
118132
position: 'fixed',
119133
top: fixedTop,
120-
width: placeholderRect.width + 'px',
121-
height: placeholderRect.height + 'px',
134+
width,
135+
height,
122136
};
123137
newState.placeholderStyle = {
124-
width: placeholderRect.width + 'px',
125-
height: placeholderRect.height + 'px',
138+
width,
139+
height,
126140
};
127141
} else if (fixedBottom !== undefined) {
142+
const width = `${placeholderRect.width}px`;
143+
const height = `${placeholderRect.height}px`;
144+
128145
newState.affixStyle = {
129146
position: 'fixed',
130147
bottom: fixedBottom,
131-
width: placeholderRect.width + 'px',
132-
height: placeholderRect.height + 'px',
148+
width,
149+
height,
133150
};
134151
newState.placeholderStyle = {
135-
width: placeholderRect.width + 'px',
136-
height: placeholderRect.height + 'px',
152+
width,
153+
height,
137154
};
138155
}
139156

0 commit comments

Comments
 (0)