Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / Javascript

Getting scrollbar height&width

3.00/5 (2 votes)
22 Nov 2009CPOL 8.8K  
function GetSBSize(){ var i = document.createElement('p'); i.style.width = '100%'; i.style.height = '200px'; var o = document.createElement('div'); o.style.position = 'absolute'; o.style.top = '0px'; o.style.l
JavaScript
function GetSBSize(){
            var i = document.createElement('p');
            i.style.width = '100%';
            i.style.height = '200px';
            var o = document.createElement('div');
            o.style.position = 'absolute';
            o.style.top = '0px';
            o.style.left = '0px';
            o.style.visibility ='hidden';
            o.style.width = '200px';
            o.style.height = '150px';
            o.style.overflow = 'hidden';
            o.appendChild(i);
            document.body.appendChild(o);
            var w1 = i.offsetWidth;
            var h1 = i.offsetHeight;
            o.style.overflow = 'scroll';
            var w2 = i.offsetWidth;
            var h2 = i.offsetHeight;
            if (w1 == w2) w2 = o.clientWidth;
            if (h1 == h2) h2 = o.clientWidth;
            document.body.removeChild(o);
            //assing to window object
            window.scrollbarWidth = w1 - w2;
            window.scrollbarHeight = h1 - h2; 
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)