Get vertical scrollbar width and example

$.scrollbarWidth = function () {
    var parent, child, width;

    if (width === undefined) {
        parent = $('<div style="width: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" });
}

 

posted @ 2015-08-04 13:15  webglcn  阅读(262)  评论(0编辑  收藏  举报