Skip to content

Instantly share code, notes, and snippets.

@blake-simpson
Created March 8, 2013 10:02
Show Gist options
  • Save blake-simpson/5115461 to your computer and use it in GitHub Desktop.
Save blake-simpson/5115461 to your computer and use it in GitHub Desktop.
A JavaScript function that checks the size of the scrollbar for the current browser and adds a class to the body allowing CSS styling. Useful when designing cross-browser for OSX where some browsers have an overlaid 0 width scrollbar and other use 15px+. The function could be modified to add a data attribute with the exact width, if you need mor…
// Discovered here then refactored: http://davidwalsh.name/detect-scrollbar-width
var checkScrollbarWidth = function() {
var $body = $("body"),
$div = $('<div class="scrollbar-measure">'),
scrollbarWidth;
$div.appendTo( $body );
scrollbarWidth = $div[0].offsetWidth - $div[0].clientWidth;
$div.remove();
$body.addClass( scrollbarWidth > 0 ? "has-scrollbar" : "no-scrollbar" );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment