[英]Get the browser viewport width and height no matter the size of the contents and without sizing to 100%?
Is there a way to get the width and height of the page viewport NOT the contents of a page without setting the body width and height to 100%?有没有办法在不将正文宽度和高度设置为 100% 的情况下获取页面视口的宽度和高度而不是页面的内容?
The answers I've found require the html and body elements to be set to 100% width and 100% height.我找到的答案要求将 html 和 body 元素设置为 100% 宽度和 100% 高度。
I already have content on the page that sometimes extends beyond the viewport size.页面上的内容有时超出视口大小。 I can't switch the body to 100% and then check the values and then revert back.
我无法将主体切换到 100%,然后检查值,然后再恢复。
In my opinion there should be a property that is simply:在我看来,应该有一个简单的属性:
document.body.viewportWidth;
document.body.viewportHeight;
That update on window resize and exclude the chrome.窗口调整大小并排除chrome的更新。
There are related questions here:这里有相关的问题:
HTML5 Canvas 100% Width Height of Viewport? HTML5 Canvas 100% 宽视口的高度?
Update:更新:
I've been using this:我一直在用这个:
var availableWidth = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
var availableHeight = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
It might be that the viewport values are correct but the element itself is growing or shrinking.可能是视口值是正确的,但元素本身正在增长或缩小。
I can't find the document.documentElement.clientWidth or document.documentElement.clientHeight in the documentElement docs though.我找不到document.documentElement.clientWidth或document.documentElement.clientHeight在documentElement文档虽然。
Update 2:更新 2:
It looks like it might be an error on my part.看起来这可能是我的错误。 I noticed in the logs that occasionally the element has no computed width or height possibly because it hasn't been added to the page yet.
我在日志中注意到,有时元素没有计算宽度或高度,可能是因为它尚未添加到页面中。
var elementWidth = parseFloat(getComputedStyle(element, "style").width);
var elementHeight = parseFloat(getComputedStyle(element, "style").height);
I think the code I'm already using above is correct.我认为我上面已经使用的代码是正确的。
Update 3:更新 3:
I found this page on getting the viewport size.我在获取视口大小时找到了这个页面。
I may not understand your question correctly, but if you're looking for the height and width of the browser window, you could use window.innerHeight
and window.innerWidth
.我可能没有正确理解你的问题,但如果你正在寻找浏览器窗口的高度和宽度,你可以使用
window.innerHeight
和window.innerWidth
。
Here is some more information on the window height and width that could be useful.以下是有关窗口高度和宽度的一些可能有用的更多信息。
Does this help?这有帮助吗?
document.documentElement.clientWidth
document.documentElement.clientHeight
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.