FatDogMark FatDogMark - 12 days ago 8
Javascript Question

How to hide scrollbar with using overflow-x:scroll?

I want the

div
to be scrollable on ipad but I don't want the scrollbar to be seen on the desktop browsers, is it possible? overflow-x:scroll, but the scrollbar is invisible? Remain the scrolling function but remove the scrollbar.
Or is it possible to use a
div
to cover the scrollbar?

Answer

Please check the fiddler code here.

Use the below code to get the scrollbar width (refferred from here)

function getScrollBarSize () {  
   var inner = document.createElement('p');  
   inner.style.width = "100%";  
   inner.style.height = "100%";  

   var outer = document.createElement('div');  
   outer.style.position = "absolute";  
   outer.style.top = "0px";  
   outer.style.left = "0px";  
   outer.style.visibility = "hidden";  
   outer.style.width = "100px";  
   outer.style.height = "100px";  
   outer.style.overflow = "hidden";  
   outer.appendChild (inner);  

   document.body.appendChild (outer);  

   var w1 = inner.offsetWidth;  
   var h1 = inner.offsetHeight;
   outer.style.overflow = 'scroll';  
   var w2 = inner.offsetWidth;  
   var h2 = inner.offsetHeight;
   if (w1 == w2) w2 = outer.clientWidth;
   if (h1 == h2) h2 = outer.clientHeight;   

   document.body.removeChild (outer);  

   return [(w1 - w2),(h1 - h2)];  
};

Set the width to the inner div as below:

$(document).ready(function() {
  $(".innerDiv").width($(".outerDiv").width() + getScrollBarSize()[0]);
                    });

There is one more link which gives the implementation of calculating the scrollbar width and height in jquery and non-jquery way.