if you are using jquery try the following:
function getScrollbarWidth() { var div = $('<div style="width:50px;height:50px;overflow:hidden;position:absolute;top:-200px;left:-200px;"><div style="height:100px;"></div></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 use this in a project I'm working on, and it works like a charm. it gets the scrollbar width:
- adding a div with overflowing contents to the body (in the invisible area, -200px top / left)
- set overflow to
hidden
- get width
- set overflow to
auto
(to get scroll bars) - get width
- align both widths to get scrollbar width
so you will need to make the width of your div ( $('#mydiv').width()
) and add the scrollbar width:
var completewidth = $('#mydiv').width() + getScrollbarWidth();
oezi source share