1
<html>
    <head> 
        <meta charset="utf-8" /> 
        <title>test</title> 
    </head> 
    <body style="background-color: red;width: 100vw;height: 100vh" id="123">  
        <script>
            window.onresize = function() {
                var res = document.getElementsByTagName('html')[0].getBoundingClientRect();
                document.getElementById("123").innerHTML=JSON.stringify(res)
            }
        </script> 
    </body>
</html>

enter image description here enter image description here

As in the above code, the width and height values corresponding to the window zoom will also change. The actual width and height cannot be obtained when there is a scroll bar. What I want to obtain is the current visible area plus the area hidden by the scroll bar, no matter how the window changes, the width and height Should be immobile

Tyler2P
  • 2,324
  • 26
  • 22
  • 31

3 Answers3

0

You can use offsetWidth and offsetHeight

<html>
    <head> 
        <meta charset="utf-8" /> 
        <title>test</title> 
    </head> 
    <body style="background-color: red;width: 100vw;height: 100vh">
        <div id="123"></div>  
        <script>
            window.onresize = function() {
                document.getElementById("123").innerHTML = document.getElementsByTagName('html')[0].offsetWidth + 'x' + document.getElementsByTagName('html')[0].offsetHeight;
            }
        </script> 
    </body>
</html>
0

You should replace html with body.

document.querySelector('body');
zhulien
  • 5,145
  • 3
  • 22
  • 36
0

window.onresize = function(){


  document.getElementById("123").innerHTML= JSON.stringify({"width" : document.body.scrollWidth , "height" : document.body.scrollHeight}) 
}
<html>
 <head> 
  <meta charset="utf-8" /> 
  <title>test</title> 
 </head> 
 <body style="background-color: red;width: auto;height: 100vh" id="123">  

 </body>
</html>

Edit: The above code will solve your issue, use body.scrollWidth instead of clientWidth

RobC
  • 22,977
  • 20
  • 73
  • 80