Javascript 使用 PHP 获取浏览器窗口大小/分辨率
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8459771/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get browser window size/resolution with PHP
提问by Pmillan
I have a page that starts with a navigation bar, with specific height, and under that I placed a code that should fill out the rest of the height. How could I code it for various resolutions? I need the "working area" size of the browser.
我有一个以导航栏开头的页面,具有特定的高度,然后在其下放置了一个代码,该代码应该填充其余的高度。我如何为各种分辨率编码?我需要浏览器的“工作区”大小。
采纳答案by Scott Greenfield
Cannot be done with PHP. Even if it could, this CSSis a far better solution.
不能用 PHP 完成。即使可以,这个 CSS也是一个更好的解决方案。
You have encountered the most annoying CSS problem in existence. There seems to be as many answers to this question as there are people asking it. The above link is the best, most cross-browser friendly solution I have found for this issue.
您遇到了现存最烦人的 CSS 问题。这个问题的答案似乎和问它的人一样多。上面的链接是我为这个问题找到的最好的、对跨浏览器最友好的解决方案。
Don't let the part about the footer confuse you. If you don't want a footer, it will still work perfectly.
不要让有关页脚的部分让您感到困惑。如果您不想要页脚,它仍然可以完美运行。
回答by Peter
$window_width = "<script type='text/javascript'>document.write(window.innerWidth);</script>";
$window_width = "<script type='text/javascript'>document.write(window.innerWidth);</script>";
回答by Sasha Shepherd
Here's how I did it. I just replaced my index.php with this simple little tool, and renamed my index.php index2.php. It just checks these things using pure javascript and passes them to your real index file as $_GET variables. What could be easier than that? All done! Took me 5 minutes, will take you 30 seconds :)
这是我如何做到的。我只是用这个简单的小工具替换了我的 index.php,并重命名了我的 index.php index2.php。它只是使用纯 javascript 来检查这些东西,并将它们作为 $_GET 变量传递给您的真实索引文件。还有什么比这更容易的?全部完成!我用了 5 分钟,你用了 30 秒 :)
<html>
<head>
<script type="text/javascript">
if(window.innerHeight > window.innerWidth){
var landscapeOrPortrait = 'portrait';
} else {
var landscapeOrPortrait = 'landscape';
}
var width = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
var height = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
window.location.replace('index2.php?width='+width+'&height='+height+'&landscapeOrPortrait='+landscapeOrPortrait);
</script>
</head>
回答by gilly3
Use absolute positioning.
使用绝对定位。
.nav {
position: absolute;
top: 0;
left: 0;
right: 0;
height: 100px;
}
.content {
position: absolute;
top: 100px;
left: 0;
right: 0;
bottom: 0;
}