用js计算滚动条的宽度
function getScrollbarWidth() {
const outer = document.createElement('div')
const inner = document.createElement('div')
outer.style.overflow = 'scroll'
document.body.appendChild(outer)
outer.appendChild(inner)
const scrollbarWidth = outer.offsetWidth - inner.offsetWidth
document.body.removeChild(outer)
return scrollbarWidth
}
短短几行就可以搞定啦~