Skip to content

Commit 12335c2

Browse files
committed
fix scroll measure not work fine on ff of win
fix ant-design/ant-design#13511
1 parent b57463c commit 12335c2

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

src/utils.js

+18-8
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,48 @@
11
import warning from 'warning';
22

3-
let scrollbarSize;
3+
let scrollbarVerticalSize;
4+
let scrollbarHorizontalSize;
45

56
// Measure scrollbar width for padding body during modal show/hide
67
const scrollbarMeasure = {
78
position: 'absolute',
89
top: '-9999px',
910
width: '50px',
1011
height: '50px',
11-
overflow: 'scroll',
1212
};
1313

1414
export function measureScrollbar(direction = 'vertical') {
1515
if (typeof document === 'undefined' || typeof window === 'undefined') {
1616
return 0;
1717
}
18-
if (scrollbarSize) {
19-
return scrollbarSize;
18+
const isVertical = direction === 'vertical';
19+
if (isVertical && scrollbarVerticalSize) {
20+
return scrollbarVerticalSize;
21+
} else if (!isVertical && scrollbarHorizontalSize) {
22+
return scrollbarHorizontalSize;
2023
}
2124
const scrollDiv = document.createElement('div');
2225
Object.keys(scrollbarMeasure).forEach(scrollProp => {
2326
scrollDiv.style[scrollProp] = scrollbarMeasure[scrollProp];
2427
});
28+
// Append related overflow style
29+
if (isVertical) {
30+
scrollDiv.style.overflowY = 'scroll';
31+
} else {
32+
scrollDiv.style.overflowX = 'scroll';
33+
}
2534
document.body.appendChild(scrollDiv);
2635
let size = 0;
27-
if (direction === 'vertical') {
36+
if (isVertical) {
2837
size = scrollDiv.offsetWidth - scrollDiv.clientWidth;
29-
} else if (direction === 'horizontal') {
38+
scrollbarVerticalSize = size;
39+
} else if (!isVertical) {
3040
size = scrollDiv.offsetHeight - scrollDiv.clientHeight;
41+
scrollbarHorizontalSize = size;
3142
}
3243

3344
document.body.removeChild(scrollDiv);
34-
scrollbarSize = size;
35-
return scrollbarSize;
45+
return size;
3646
}
3747

3848
export function debounce(func, wait, immediate) {

0 commit comments

Comments
 (0)