6

I want to disable the scroll in one of my pages. I don't want to write

scroll(0,0) or scrollTo(0,0)

in the scroll event which will give a 'jumpy' nature. Can't I have something like

event.preventDefault()

for canceling the event?

Bill the Lizard
  • 369,957
  • 201
  • 546
  • 842
NLV
  • 19,811
  • 35
  • 115
  • 178
  • See http://stackoverflow.com/questions/1459676/prevent-scroll-bubbling-from-element-to-window/1460020#1460020 – Crescent Fresh Aug 04 '10 at 11:38
  • http://stackoverflow.com/questions/4770025/how-to-disable-scrolling-temporarily although that is to temporarily disable scrolling, you can set it to permanently disable it. You should use that instead of using overflow: hidden because Chrome has a bug that you can still scroll on overflow: hidden pages using the mousewheel, arrow keys, pgup and pgdn keys, and touch scrolling if you're on a touch device. The accepted answer in that link provides a way that effectively disables it. – markasoftware Jul 22 '13 at 17:36

4 Answers4

6
document.body.style.overflow = 'hidden';
Bill the Lizard
  • 369,957
  • 201
  • 546
  • 842
Marcelo
  • 9,161
  • 3
  • 31
  • 40
3

The only acceptable way of preventing scrolling is making the content fit inside the window. Anything else, and you're pissing users off.

You can also use position: fixed CSS - but still, if it's larger than the window, the rest is unusable, so you might as well follow the suggestion #1.

Amadan
  • 169,219
  • 18
  • 195
  • 256
  • There actually are many uses for cutting off content. My site uses css 3d transforms, and for some reason a useless part of it extends 10-20 pixels beyond the end of the page. – markasoftware Jul 22 '13 at 17:39
  • Quite the blanket statement Amadan. In my case, I want to shut down the auto scrolling (which is "pissing off" my users.) This occurs when elements from the list are added / removed. Please don't assume that a particular technique is no good, just because this particular use case could be criticized. – redevill Aug 20 '20 at 22:22
0

I wonder if overflow: hidden; will work by putting it on the html, body selector in CSS?

dsamarin
  • 303
  • 2
  • 6
0

Create a div that fills the whole area and set that to overflow: hidden. That way, you will never get scroll bars and no scroll events will be created.

Aaron Digulla
  • 297,790
  • 101
  • 558
  • 777