让前端飞

取滚动条宽度

2019-01-07  本文已影响2人  歇歇

原理

所以在元素没有边框时,等式offsetWidth - clientWidth = scrollWidth成立

实现

/**
 * @description 获取滚动条宽度
 */
function getScrollWidth() {
  let node = document.createElement('div')
  node.style = 'overflow: scroll; visibility: hidden;'
  document.body.append(node)
  let w = node.offsetWidth - node.clientWidth
  node.remove()
  return w
}
上一篇 下一篇

猜你喜欢

热点阅读