简体   繁体   English

如何获取页面的可视部分的宽度和高度?

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

How to get them by using JS and jQuery? 如何通过使用JS和jQuery获得它们?

I know $(window).innerWidth() can get. 我知道$(window).innerWidth()可以得到。 But I don't hope it contains the width or height of the scroll bar. 但是我不希望它包含滚动条的宽度或高度。

Thank you! 谢谢!

From the jQuery website : jQuery网站

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

If you add overflow:hidden to the body of this page (so there's no scrollbar), then run $(window).width() in a JS console, notice this value increases! 如果在页面正文中添加了overflow:hidden (因此没有滚动条),然后在JS控制台中运行$(window).width() ,请注意此值会增加!

i Have found the best way is with Javascript. 我发现最好的方法是使用Javascript。

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

//code goes here
</script>

Bear in mind that when you use these, they return an integer so if you are going to use them to apply a style to another object or element then you will have to append them as so: 请记住,当使用它们时,它们返回一个整数,因此,如果要使用它们将样式应用于另一个对象或元素,则必须将它们附加为:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM