Check whether HTML element has scrollbars using JavaScript - GeeksforGeeks

20 Sep, 2019
Given an HTML document and the task is to identify whether a particular element has scrollBars or not. There are two approaches to solve this problem which are discussed below:
Approach 1:
Select the particular element.
Get the element.scrollWidth and .clientWidth property for horizontal scrollbar.
Calculate the scrollWidth>clientWidth.
If the value comes true then horizontal scrollbar is present else not.
Do the same process to check vertical scrollbar.
Example 1: This example implements the above approach.
<!DOCTYPE HTML>  <html>    <head>     <title>          Check whether HTML element has         scrollbars using JavaScript    </title>              <style>        #div {            width:200px;            height:150px;            overflow:auto;             text-align:justify;        }        #GFG {            font-size: 24px;            font-weight: bold;            color: green;        }    </style></head>      <body>    <center>        <h1 style = "color:green;" >              GeeksforGeeks          </h1>                  <h3>            Click on the button to check            for the scrollBars        </h3>              <div id="div">            This course is for all those people who want to            learn Data Structures and Algorithm from basic            to advance level. We don't expect you to have            any prior knowledge on Data Structure and             Algorithm, but a basic prior knowledge of any            programming language ( C++ / Java) will be            helpful. This course gives you the flexibility            of learning, under this program you can study             your course whenever you feel like, you need            not hurry or puzzle yourself.        </div>        <br>                  <button onclick = "GFG_Fun()">            Click Here!        </button>                  <div id = "GFG"></div>                  <script>            function GFG_Fun() {                var div = document.getElementById('div');                var hs = div.scrollWidth > div.clientWidth;                var vs = div.scrollHeight > div.clientHeight;                                  document.getElementById('GFG').innerHTML                         = "Horizontal Scrollbar - " + hs                        +"<br>Vertical Scrollbar - " + vs;            }        </script>     </center></body>    </html>
Output:
Before clicking on the button:
After clicking on the button:
Approach 2:
Select the particular element.
Use the scrollTop and scrollLeft properties.
If these are greater than 0, scrollbars are present.
If these are 0, then first set them to 1, and test again to know if getting a result of 1.
Finally, set them back to 0.
Example 2: This example using the approach discussed above.
<!DOCTYPE HTML>  <html>    <head>     <title>          Check whether HTML element has         scrollbars using JavaScript    </title>              <style>        #div {            width:200px;            height:200px;            overflow:none;             text-align:justify;        }        #GFG {            font-size: 24px;            font-weight: bold;            color: green;        }    </style></head>      <body>    <center>        <h1 style = "color:green;" >              GeeksforGeeks          </h1>                  <h3>            Click on the button to check            for the scrollBars        </h3>              <div id="div">            This course is for all those people who want to            learn Data Structures and Algorithm from basic            to advance level. We don't expect you to have            any prior knowledge on Data Structure and             Algorithm, but a basic prior knowledge of any            programming language ( C++ / Java) will be            helpful.         </div>        <br>                  <button onclick = "GFG_Fun()">            Click Here!        </button>                  <div id = "GFG"></div>                  <script>            function checkScrollBar(element, dir) {                dir = (dir === 'vertical') ?                            'scrollTop' : 'scrollLeft';                                  var res = !! element[dir];                                  if (!res) {                    element[dir] = 1;                    res = !!element[dir];                    element[dir] = 0;                }                return res;            }                          function GFG_Fun() {                var div = document.getElementById('div');                var hs = checkScrollBar(div, 'horizontal');                var vs = checkScrollBar(div, 'vertical');                                  document.getElementById('GFG').innerHTML                         = "Horizontal Scrollbar - " + hs                        +"<br>Vertical Scrollbar - " + vs;            }        </script>     </center></body>    </html>
Output:
Before clicking on the button:
After clicking on the button:
JavaScript
Web Technologies
Web technologies Questions
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Difference between var, let and const keywords in JavaScript
Convert a string to an integer in JavaScript
Differences between Functional Components and Class Components in React
How to append HTML code to a div using JavaScript ?
How to Open URL in New Tab using JavaScript ?
Top 10 Front End Developer Skills That You Need in 2022
Installation of Node.js on Linux
Top 10 Projects For Beginners To Practice HTML and CSS Skills
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?