Method 1: Overriding the window.onscroll function
The window.onscroll event fires when the window has been scrolled. Overriding this function and setting it to a fixed position every time the scroll happens will effectively disable the scroll effect.
The current scroll position from the top is found by using the window.pageYOffset and the document.documentElement.scrollTop values. These 2 properties returns the current y scroll position. They are used together using the OR operator as one of them may return 0 on certain browsers.
Similarly, the current scroll position from the left is found by using the window.pageXOffset and the document.documentElement.scrollLeft values. These 2 properties returns the current x scroll position.
The window.scrollTo() method is then used with these 2 values to set the scroll position of the current page to that value.
To enable the scrolling back, window.onscroll is overridden with a blank function. This will enable the scrolling of the page again.
Example: Overriding the window.onscroll function
Method 2: Setting the height of the body to 100% and overflow to hidden
In this method, a new CSS class is created where the height is set to 100% and the scroll bar is disabled by setting the overflow property to hidden.
Whenever scrolling has to be disabled, this class is added to the body using the document.body.classList.add(“classname”) method. This method adds the specified class name to the body element’s class list.
To enable the scrolling back, this class is removed from the body using the document.body.classList.remove(“classname”) method. This method removes the specified class name to the body element’s class list. This will enable the scrolling of the page again.
Example: Setting the height of the body to 100% and overflow to hidden
- Parallax scrolling effect using CSS.
- HTML | <frame> scrolling Attribute
- How to change opacity while scrolling the page?
- HTML | <iframe> scrolling Attribute
- HTML5 Game Development | Infinitely Scrolling Background
- How to disable a link using only CSS?
- How to disable a CSS :hover effect?
- How to disable tabs in Bootstrap ?
- How to disable text selection highlighting using CSS?
- How to disable zoom on a mobile web page using CSS?
- How to disable resizable property of textarea using CSS?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.