2021-12-12 23:28:03 +08:00
|
|
|
import { isClient } from '@vueuse/core'
|
2020-07-28 00:32:04 +08:00
|
|
|
|
|
|
|
let scrollBarWidth: number
|
|
|
|
|
2021-09-04 19:29:28 +08:00
|
|
|
export default function (): number {
|
2021-12-12 23:28:03 +08:00
|
|
|
if (!isClient) return 0
|
2020-07-28 00:32:04 +08:00
|
|
|
if (scrollBarWidth !== undefined) return scrollBarWidth
|
|
|
|
|
|
|
|
const outer = document.createElement('div')
|
|
|
|
outer.className = 'el-scrollbar__wrap'
|
|
|
|
outer.style.visibility = 'hidden'
|
|
|
|
outer.style.width = '100px'
|
|
|
|
outer.style.position = 'absolute'
|
|
|
|
outer.style.top = '-9999px'
|
|
|
|
document.body.appendChild(outer)
|
|
|
|
|
|
|
|
const widthNoScroll = outer.offsetWidth
|
|
|
|
outer.style.overflow = 'scroll'
|
|
|
|
|
|
|
|
const inner = document.createElement('div')
|
|
|
|
inner.style.width = '100%'
|
|
|
|
outer.appendChild(inner)
|
|
|
|
|
|
|
|
const widthWithScroll = inner.offsetWidth
|
2021-09-15 08:30:30 +08:00
|
|
|
outer.parentNode?.removeChild(outer)
|
2020-07-28 00:32:04 +08:00
|
|
|
scrollBarWidth = widthNoScroll - widthWithScroll
|
|
|
|
|
|
|
|
return scrollBarWidth
|
|
|
|
}
|