7

How do I disable body's scroll bar, but let it stay visible on screen? As an example see facebook's theatre mode:

facebook screen

lucas clemente
  • 5,645
  • 7
  • 37
  • 57
  • Possible duplicate of [Just disable scroll not hide it?](https://stackoverflow.com/questions/8701754/just-disable-scroll-not-hide-it) – Jordan Running Sep 08 '17 at 15:19

2 Answers2

17

This is the only CSS you will ned to force the scrollbars:

html{
    overflow-y: scroll;
}

The scrollbars will be disabled if the content is smaller than the window, so you will have to use some script to resize the body to be at least 1px less then the window height:

$("body").css({ "height" : ($(window).height() - 1) + 'px', "overflow": "hidden" });
lucas clemente
  • 5,645
  • 7
  • 37
  • 57
Ricardo Souza
  • 14,681
  • 6
  • 33
  • 64
  • This does the job really well. However instead of using JS for defining body height, I find it cleaner to go with css. With little playing around, I found two options: Apply position:fixed; to html tag, along with overflow-y:scroll; Or, if you are using a framework which already adds overflow:hidden, to your body tag (I assume most people who searched this issue, have it because overflow: hidden; on body), you could just define body height with 100vh. Don't forget to remove those styles when modal is closed. – mzrnsh Feb 23 '16 at 02:45
  • Good catch. You shoul add your own answer, so I can upvote it. – Ricardo Souza Feb 23 '16 at 23:48
  • Thanks but I am not done with my solution yet :) I am trying to apply this to a very long page so most of the times users will first scroll then open modal and all my options cause either scrollbar to be still active, or scroll the user to top and then deactivate scrollbar. I checked what facebook does and they use position fixed plus adding top: xxxxx pixels (based on how far you have scrolled already) on content wrapper (not body in their case). When I am done I may add that as an answer – mzrnsh Feb 24 '16 at 01:35
-4
overflow-y: scroll; 

That should be what you're looking for.

Chad
  • 4,942
  • 3
  • 22
  • 34