Get the size of a document when window resize

左心房为你撑大大i 提交于 2021-02-05 06:51:28

问题


I can find the size of window, when i resize my window.like this

<script type="text/javascript">

    jQuery(window).resize(function () {
      var width = jQuery(window).width();
      var height = jQuery(window).height();
      console.log(width);
      console.log(height);
    })
</script>

Now i want to get the document size when i resize the window. How can i get the size every time when resize the window.


回答1:


$(window).width(); // returns width of browser viewport

$(document).width(); // returns width of HTML document

http://api.jquery.com/width/




回答2:


You can use the document object:

var width = jQuery(document).width();
var height = jQuery(document).height();

Demo: http://jsfiddle.net/wyHwp/

As you see, the size of the document is the same value while the window is smaller. When the window gets larger than the document needs, the document will stretch to the size of the window.

You can also get the size of the document.body element:

var width = jQuery(document.body).width();
var height = jQuery(document.body).height();

The difference is that you get the height of the body element even if the window is higher, i.e. the body element doesn't automatically stretch to the bottom of the window.




回答3:


Not sure if you want this:

jQuery(window).resize(function () {
   var winwidth = jQuery(window).width();
   var winheight = jQuery(window).height();
   var docwidth = jQuery(document).width();
   var docheight = jQuery(document).height();
   console.log('window width is -> ' + winwidth + 'window height is -> ' + winheight);
   console.log('document width is -> ' + docwidth + 'document height is -> ' + docheight);
}).resize();
//-^^^^^^^^----this will log everytime on doc ready



回答4:


Modify your example code to get it at the same time.

<script type="text/javascript">

    jQuery(window).resize(function () {
      var width = jQuery(window).width();
      var height = jQuery(window).height();
      var documentWidth = jQuery(document).width();
      var documentHeight = jQuery(document).height();
      console.log(width);
      console.log(height);
    })
</script>


来源:https://stackoverflow.com/questions/15269850/get-the-size-of-a-document-when-window-resize

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!