| 1 | let scrollBarWidth; |
| 2 | |
| 3 | export function getScrollBarWidth() { |
| 4 | if (scrollBarWidth !== undefined) return scrollBarWidth; |
| 5 | |
| 6 | const outer = document.createElement('div'); |
| 7 | outer.className = 'el-scrollbar__wrap'; |
| 8 | outer.style.visibility = 'hidden'; |
| 9 | outer.style.width = '100px'; |
| 10 | outer.style.position = 'absolute'; |
| 11 | outer.style.top = '-9999px'; |
| 12 | document.body.appendChild(outer); |
| 13 | |
| 14 | const widthNoScroll = outer.offsetWidth; |
| 15 | outer.style.overflow = 'scroll'; |
| 16 | |
| 17 | const inner = document.createElement('div'); |
| 18 | inner.style.width = '100%'; |
| 19 | outer.appendChild(inner); |
| 20 | |
| 21 | const widthWithScroll = inner.offsetWidth; |
| 22 | scrollBarWidth = widthNoScroll - widthWithScroll; |
| 23 | outer.parentNode.removeChild(outer); |
| 24 | |
| 25 | return scrollBarWidth; |
| 26 | } |