43

Possible Duplicate:
How to disable browser or element scrollbar, but allow scrolling with wheel or arrow keys?

I was able to disable vertical scrollbars in a grid by setting the CSS property overflow-y: hidden. However, this removed the ability to scroll the contents with the mouse wheel as well.

Is there a way to not show the scrollbars but still allow the contents to be scrolled through mouse wheel or arrow keys?

Community
  • 1
  • 1
Sameer
  • 751
  • 3
  • 9
  • 17

2 Answers2

63

There are Javascript methods, see the thread you duplicated.

A better solution is to set the target div to overflow:scroll, and wrap it inside a second element that is 8px narrower, who's overflow:hidden.

The target element will have a hidden scrollbar. The mousewheel will work, but the scroll bar will not show.

<div style='overflow:hidden; width:200px;'>
   <div style='overflow:scroll; width:208px'>
      My mousewheel scrollable content here....
   </div>
</div>

Note that 8px as the width of the scrollbar is a random number - it's probably a lot more, and it could require per browser CSS.

Still better than JS in my book.

SamGoody
  • 13,758
  • 9
  • 81
  • 91
  • 2
    Here's [how to calculate scrollbar sizes from JavaScript](http://stackoverflow.com/questions/986937/javascript-get-the-browsers-scrollbar-sizes). – natevw May 22 '12 at 15:44
  • I was avoiding JS, as many users have NoScript or other unusual setups - pure CSS is more reliable. – SamGoody May 23 '12 at 08:27
  • I was going for this solution but found out my scroll bar gets hidden automatically as my absolute position inner div sets itself to 215px (15px scrollbar) while keeping left: 0. Could I have any potential problems with it? I'm not quite sure which rules are making it work though. – Vic Goldfeld Aug 07 '12 at 11:51
  • Great solution. A little improvement to it is to use relative width for the inner div, i.e.
    . This makes sure that when the outer div gets resized the inner div adapts to the new width too.
    – dave Aug 09 '12 at 07:45
  • 1
    Try your demo in Chrome/Safari: highlight/select a line and drag your mouse to the right and you'll see the scrollbar. Or use a textarea instead of the inner div and then fill it with some text. Then use the keyboard keys `Page Up` and `Page Down`. – Mori Dec 06 '12 at 16:24
  • Another way to solve it in this manner (since this is the first result you get from ggl): In the child div, you set margin-bottom to -100px and padding-bottom to 100px (or right if you have a vertical scrollbar), box-sizing to border-box and height 100%. Overflow stays the same, this way you're not stuck with fixed width / height issues. – smets.kevin Nov 09 '13 at 07:42
  • If i want to use the value for main div width is "100% and height is "100%" then how much should i specify width in percentage for inner div and height also in percentage? – SHEKHAR SHETE Nov 11 '13 at 07:21
  • meh. Works like a charm with chrome but can't scroll anymore on Firefox. I can scroll with keys but not with the wheel.. – Michael De Keyser Jun 21 '14 at 11:01
  • Not working for relative widths. – 71GA Dec 06 '15 at 20:26
1

You could use jScrollPane which allows you to replace the browser scrollbars with custom ones:

Since you can style these custom scrollbars with CSS you could easily make them disappear (try something like: .jScrollPaneTrack { display: none; })

Akhil Jain
  • 13,872
  • 15
  • 57
  • 93
vitch
  • 3,215
  • 3
  • 25
  • 26
  • This is an opposite answer on the question was asked! The reason why they require ability to be able to scroll with mouse wheel (like natural scrolling) and have custom scroll bars is, for instance, because jScrollPane (and many others) won't work with touchable devices. Btw, one more reason why I would not recommend to use jScrollPane is at least because its samples hosted on some environment which even does not support case-insensetive urls! Which is pretty confusing when typing manually to test on iPhone or any other device. – Agat Mar 27 '13 at 15:51
  • 2
    It seems to me it answers the question. jScrollPane *does* work with touch devices. And certainly works with the mouse wheel and arrow keys (which is what the question actually asks). Your other reason is because it's hosted on *nix (like the vast majority of the web) and you have trouble reading or typing capitals??! – vitch Mar 28 '13 at 16:57
  • Its behaviour confirmed my thoughts. I don't know in what conditions the pane must work on touchable devices, but on this link: http://www.kelvinluck.com/assets/jquery/jScrollPane/basic.html it DID NOT work for my iPhone. Neither of those samples. It did not work neither when trying to swipe content of the blocks, nor allowed to drag scroller. It was working only on clicking on the bar (page up/page down). And I've "minused" your answer, because I was looking for the same solution which author was looking for, but this suggestion solves nothing in that context. Sorry, but that's my oppinion. – Agat Mar 29 '13 at 12:48