Skip to content

Commit 6d35082

Browse files
committed
fix(useAffix): enhance scroll handling for both horizontal and vertical page scrolls
1 parent d9c46ca commit 6d35082

File tree

1 file changed

+30
-12
lines changed

1 file changed

+30
-12
lines changed

packages/components/table/hooks/useAffix.ts

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export default function useAffix(props: TdBaseTableProps, { showElement }: { sho
1313
const tableContentRef = useRef<HTMLDivElement>(null);
1414
const lastTableScrollLeftRef = useRef<number>(0);
1515
const lastPageScrollLeftRef = useRef<number>(0);
16+
const lastPageScrollTopRef = useRef<number>(0);
1617

1718
// 吸顶表头
1819
const affixHeaderRef = useRef<HTMLDivElement>(null);
@@ -68,25 +69,42 @@ export default function useAffix(props: TdBaseTableProps, { showElement }: { sho
6869
};
6970

7071
const onPageHorizontalScroll = () => {
71-
if (!isAffixed || !props.fixedRows?.length) return;
72+
if (!isAffixed) return;
7273

7374
const pageScrollLeft = window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft || 0;
74-
if (lastPageScrollLeftRef.current === pageScrollLeft) return;
75-
lastPageScrollLeftRef.current = pageScrollLeft;
75+
const pageScrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0;
76+
77+
if (lastPageScrollLeftRef.current !== pageScrollLeft) {
78+
lastPageScrollLeftRef.current = pageScrollLeft;
7679

77-
const toUpdateScrollElement = [affixHeaderRef.current, affixFooterRef.current, horizontalScrollbarRef.current];
78-
toUpdateScrollElement.forEach((element) => {
79-
if (element) {
80+
const toUpdateScrollElement = [affixHeaderRef.current, affixFooterRef.current, horizontalScrollbarRef.current];
81+
toUpdateScrollElement.forEach((el) => {
82+
if (!el) return;
8083
// 固定行会使用绝对定位,吸顶表头的位置计算需要考虑页面级别的滚动偏移
81-
if (props.fixedRows.length) {
84+
if (pageScrollLeft) {
8285
// eslint-disable-next-line no-param-reassign
83-
element.style.marginLeft = `-${pageScrollLeft}px`;
84-
} else if (element.style.marginLeft) {
86+
el.style.marginLeft = `-${pageScrollLeft}px`;
87+
} else if (el.style.marginLeft) {
8588
// eslint-disable-next-line no-param-reassign
86-
element.style.marginLeft = '';
89+
el.style.marginLeft = '';
8790
}
88-
}
89-
});
91+
});
92+
}
93+
94+
if (lastPageScrollTopRef.current !== pageScrollTop) {
95+
lastPageScrollTopRef.current = pageScrollTop;
96+
const toUpdateScrollElement = [affixHeaderRef.current, affixFooterRef.current];
97+
toUpdateScrollElement.forEach((el) => {
98+
if (!el) return;
99+
if (pageScrollTop) {
100+
// eslint-disable-next-line no-param-reassign
101+
el.style.marginTop = `-${pageScrollTop}px`;
102+
} else if (el.style.marginTop) {
103+
// eslint-disable-next-line no-param-reassign
104+
el.style.marginTop = '';
105+
}
106+
});
107+
}
90108
};
91109

92110
// 吸底的元素(footer、横向滚动条、分页器)是否显示

0 commit comments

Comments
 (0)