Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Show current scrollbar width</title>
</head>
<body>
  <title>Show current scrollbar width</title>
  <div>Try: Page zoom (ctrl-/+), Pinch zoom (), Switching Tablet mode (Win10), changing mouse settings (OSX), Changing scrollbar width (theme), Changing resolution (pixel density)</div>
  <button onclick="logWidth('click')">scrollbarWidth()</button>
</body>
</html>
 
setup();
function scrollbarWidth() {
  var scrollDiv = document.createElement("div");
  scrollDiv.style.cssText = 'width:100px;height:100px;overflow:scroll !important;position:absolute;top:-9999px';
  document.body.appendChild(scrollDiv);
  var result = scrollDiv.offsetWidth - scrollDiv.clientWidth;
  document.body.removeChild(scrollDiv);
  return result;
}
function logWidth(context) {
  var div = document.createElement('div');
  div.textContent = 'Scrollbar width is ' + scrollbarWidth() + 'px at ' + context;
  document.body.appendChild(div);
}
function oriented() {
  logWidth('orientationchange');
}
function resized() {
  logWidth('resized');
}
function showed() {
  logWidth('pageshow');
}
function setup() {
  if (document.body) {
    logWidth('init');
  }
  window.addEventListener('resize', resized);
  window.addEventListener('pageshow', pageshow);
  window.addEventListener('orientationchange', oriented);
}
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
robocatpro
0viewers