2013-03-07 34 views
2

我能找到窗口的大小,當我調整我window.like這獲取文檔的大小時窗口調整

<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> 

現在我想,當我調整窗口的大小,以獲取文檔的大小。如何在每次調整窗口大小時獲取大小。

+0

這是一個重複的問題(http://stackoverflow.com/questions/3044573/using-jquery-to-get [呢?] -size-of-viewport) – 2013-03-07 11:25:23

回答

5

$(window).width(); //返回瀏覽器視口的寬度

$(document).width(); HTML文檔的//返回寬度

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

+0

我知道$(document).width()和$(document).height()給出了HTML文檔的寬度和高度,但是當窗口被調整大小時,是否有任何方法來調整大小html文件。 – Ranjit 2013-03-07 11:33:34

+0

使用'percentage'或'em'聲明內容的寬度,網站將縮放到窗口寬度。 – Stefan 2013-03-07 12:31:11

3

可以使用document對象:

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

演示:http://jsfiddle.net/wyHwp/

正如你看到的,文件的大小是相同的值,而窗口更小。當窗口大於文檔需要時,文檔將拉伸到窗口的大小。

您還可以得到document.body元素的大小:

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

不同的是,你即使窗口更高body元素的高度,即body元素不會自動伸展到窗口的底部。

1

修改您的示例代碼以同時獲取它。

<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> 
2

不知道,如果你想這樣的:

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 
相關問題