How to hide scroll bar for inactivity?

27 Apr, 2020
There are many ways to hide the scroll bar when the page is inactive. One such way is using the onscroll, onmousewheel, onclick and onmousemove events, which help us to achieve our goal using basic HTML and JavaScript.
Approach:
The onscroll event is used to disable the scroll bar.
The onscroll event acts as soon as the page is scrolled. So, setTimeout() method is used to provide a delay to hide the scroll bar, so that we are able to scroll down first.
The time can be adjusted according to our requirement.
onmousemove event is used to enable scrolling as soon as the mouse pointer is moved. onclick event is used to enable scrolling as soon as the user clicks. onmousewheel event is used to enable scrolling as soon as the page is scrolled down. Thus, these events help us to enable scrolling when the page becomes active again.
Example 1: As soon as the mouse moves, the mouse is scrolled, or the user clicks, the enableScrolling() function is called which allows us to scroll down. When the user tries to scroll down, the disableScrolling() function is called, which makes the scroll bar disappear after 1000ms. This time can be varied. To enable the scroll bar again, move the mouse pointer, or click, or scroll the mouse pointer to call the enableScrolling() function. The styling of the text has been done using the ‘style’ tag. The HTML and JavaScript code is as follows:
<!DOCTYPE html><html>  <head>    <meta charset="UTF-8">    <title>        How to hide scroll bar        for inactivity?    </title>          <style>        p {            font-size: 2rem;        }    </style></head>  <body onscroll="disableScrolling()"         onmousewheel="enableScrolling()"          onclick="enableScrolling()"          onmousemove="enableScrolling()">          <p>        Geeks for Geeks is a Computer Science        Portal created with the goal of        providing well written, well thought        and well-explained solutions for        selected questions. Geeks for Geeks         has covered everything, ranging from        algorithms and data structure courses        to competitive exam preparation courses.         Geeks for Geeks is in true sense a        a haven for geeks, where Tech lovers can        come together and share their knowledge.    </p>      <script>              // JavaScript code        function disableScrolling() {            setTimeout(function() {                document.body.style.overflow = 'hidden';            }, 1000);        }                  function enableScrolling() {            document.body.style.overflow = '';        }    </script></body>  </html>
Output:
Example 2: This example hides the scrollbar on the image.
<!DOCTYPE html><html>  <head>    <meta charset="UTF-8">    <title>        How to hide scroll bar        for inactivity?    </title>      <style>        img {            display: block;            margin-left: auto;            margin-right: auto;            width: 150%;        }    </style></head>  <body onscroll="disableScrolling()"         onmousewheel="enableScrolling()"          onclick="enableScrolling()"          onmousemove="enableScrolling()">      <img src="https://media.geeksforgeeks.org/wp-content/cdn-uploads/20191024224332/Geeksforgeeks-Paid-Online-Courses.png" alt="GfG">      <script>              // JavaScript code        function disableScrolling() {            setTimeout(function() {                document.body.style.overflow = 'hidden';            }, 1000);        }                  function enableScrolling() {            document.body.style.overflow = '';        }    </script></body>  </html>
Output:
The main disadvantage of hiding the scroll bar for inactivity is that as soon as the scrollbar hides, the contents of the body ‘jump’ to fill up the space that was occupied by the scrollbar. Thus, it does not appear very appealing to the user.
shs_
CSS-Misc
HTML-Misc
JavaScript-Misc
Picked
CSS
HTML
JavaScript
Web Technologies
Web technologies Questions
Write From Home
HTML
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to set space between the flexbox ?
Design a Tribute Page using HTML & CSS
How to position a div at the bottom of its container using CSS?
How to Upload Image into Database and Display it using PHP ?
How to select all child elements recursively using CSS?
REST API (Introduction)
Hide or show elements in HTML using display property
How to set the default value for an HTML <select> element ?
How to set input type date in dd-mm-yyyy format using HTML ?
HTTP headers | Content-Type