$.scrollbarWidth = function () { var parent, child, width; if (width === undefined) { parent = $('<div style="50px;height:50px;overflow:auto"><div/></div>').appendTo('body'); child = parent.children(); width = child.innerWidth() - child.height(99).innerWidth(); parent.remove(); } return width; };
Usage
function setWidth() { var scrollbarwidth = $.scrollbarWidth(); $('#targetDiv').css({ 'width': ($('#srcDiv').width() - scrollbarwidth) + "px" }); }