用js计算滚动条宽度

用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
}

短短几行就可以搞定啦~

参考资料: https://www.w3cplus.com/javascript/scroll-to-the-future-modern-javascript-css-scrolling-implementations.html

猜你喜欢

转载自blog.csdn.net/qq_29055201/article/details/84878045