Is there an easy way to get the width of a scrollbar using javascript / jquery ? I need to get the width of a div that overflows + whatever width the scroll bar is.
if you're using jquery, try this:
function getScrollbarWidth()
{
var div = $('
');
$('body').append(div);
var w1 = $('div', div).innerWidth();
div.css('overflow-y', 'auto');
var w2 = $('div', div).innerWidth();
$(div).remove();
return (w1 - w2);
}
i'm using this in the project i'm working on and it works like a charm. it gets the scrollbar-width by:
hidden
auto
(to get scrollbars)so what you'll have to do is getting the width of your div ($('#mydiv').width()
) and add the scrollbar-width:
var completewidth = $('#mydiv').width() + getScrollbarWidth();