ant-design-vue/components/_util/getScrollBarSize.ts

39 lines
992 B
TypeScript
Raw Normal View History

2020-09-30 09:35:51 +00:00
let cached: number | undefined;
2018-03-05 11:05:23 +00:00
2020-09-30 09:35:51 +00:00
export default function getScrollBarSize(fresh?: boolean) {
2018-03-05 11:05:23 +00:00
if (fresh || cached === undefined) {
2019-01-12 03:33:27 +00:00
const inner = document.createElement('div');
inner.style.width = '100%';
inner.style.height = '200px';
2018-03-05 11:05:23 +00:00
2019-01-12 03:33:27 +00:00
const outer = document.createElement('div');
const outerStyle = outer.style;
2018-03-05 11:05:23 +00:00
2019-01-12 03:33:27 +00:00
outerStyle.position = 'absolute';
2020-09-30 09:35:51 +00:00
outerStyle.top = '0';
outerStyle.left = '0';
2019-01-12 03:33:27 +00:00
outerStyle.pointerEvents = 'none';
outerStyle.visibility = 'hidden';
outerStyle.width = '200px';
outerStyle.height = '150px';
outerStyle.overflow = 'hidden';
2018-03-05 11:05:23 +00:00
2019-01-12 03:33:27 +00:00
outer.appendChild(inner);
2018-03-05 11:05:23 +00:00
2019-01-12 03:33:27 +00:00
document.body.appendChild(outer);
2018-03-05 11:05:23 +00:00
2019-01-12 03:33:27 +00:00
const widthContained = inner.offsetWidth;
outer.style.overflow = 'scroll';
let widthScroll = inner.offsetWidth;
2018-03-05 11:05:23 +00:00
if (widthContained === widthScroll) {
2019-01-12 03:33:27 +00:00
widthScroll = outer.clientWidth;
2018-03-05 11:05:23 +00:00
}
2019-01-12 03:33:27 +00:00
document.body.removeChild(outer);
2018-03-05 11:05:23 +00:00
2019-01-12 03:33:27 +00:00
cached = widthContained - widthScroll;
2018-03-05 11:05:23 +00:00
}
2019-01-12 03:33:27 +00:00
return cached;
2018-03-05 11:05:23 +00:00
}