vben
2020-10-27 9ee0a561ae3d4b11164a65d8d6813a5315065422
提交 | 用户 | age
2f6253 1 import { isWindow } from '/@/utils/is';
2
3 let scrollBarWidth: number;
4
5 export default function (): number {
6   if (!isWindow) return 0;
7   if (scrollBarWidth !== undefined) return scrollBarWidth;
8
9   const outer = document.createElement('div');
10   outer.className = 'scrollbar__wrap';
11   outer.style.visibility = 'hidden';
12   outer.style.width = '100px';
13   outer.style.position = 'absolute';
14   outer.style.top = '-9999px';
15   document.body.appendChild(outer);
16
17   const widthNoScroll = outer.offsetWidth;
18   outer.style.overflow = 'scroll';
19
20   const inner = document.createElement('div');
21   inner.style.width = '100%';
22   outer.appendChild(inner);
23
24   const widthWithScroll = inner.offsetWidth;
25   const parentNode = outer.parentNode;
26   parentNode && parentNode.removeChild(outer);
27   scrollBarWidth = widthNoScroll - widthWithScroll;
28
29   return scrollBarWidth;
30 }