Skip to content

Instantly share code, notes, and snippets.

@StevenBlack
Created September 27, 2010 13:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save StevenBlack/598993 to your computer and use it in GitHub Desktop.
Save StevenBlack/598993 to your computer and use it in GitHub Desktop.
Calculate scrollbar width
// from http://jdsharp.us/jQuery/minute/calculate-scrollbar-width.php
function scrollbarWidth() {
var div = $('<div style="width:50px;height:50px;overflow:hidden;position:absolute;top:-200px;left:-200px;"><div style="height:100px;"></div>');
// Append our div, do our calculation and then remove it
$('body').append(div);
var w1 = $('div', div).innerWidth();
div.css('overflow-y', 'scroll');
var w2 = $('div', div).innerWidth();
$(div).remove();
return (w1 - w2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment