簡體   English   中英

如何獲取頁面的可視部分的寬度和高度?

[英]How to get page's visual section's width and height?

如何通過使用JS和jQuery獲得它們?

我知道$(window).innerWidth()可以得到。 但是我不希望它包含滾動條的寬度或高度。

謝謝!

jQuery網站

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

如果在頁面正文中添加了overflow:hidden (因此沒有滾動條),然后在JS控制台中運行$(window).width() ,請注意此值會增加!

我發現最好的方法是使用Javascript。

<script type="text/javascript">
var height = document.body.offsetHeight;
var width = document.body.offsetWidth;

//code goes here
</script>

請記住,當使用它們時,它們返回一個整數,因此,如果要使用它們將樣式應用於另一個對象或元素,則必須將它們附加為:

var newHeight=height + 'px';
var newWidth=width + 'px';

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM