js获取滚动条的宽度

function getScrollWidth() {
  var noScroll, scroll, oDiv = document.createElement("DIV");
  oDiv.style.cssText = "position:absolute; top:-1000px; width:100px; height:100px; overflow:hidden;";
  noScroll = document.body.appendChild(oDiv).clientWidth;
  oDiv.style.overflowY = "scroll";
  scroll = oDiv.clientWidth;
  document.body.removeChild(oDiv);
  return noScroll-scroll;
}

//思路就是设置一个div没有滚动条的,获取其宽度,然后再让其拥有滚动条,在获取宽度,取差值

你可能感兴趣的:(js获取滚动条的宽度)