PHP 条件取决于窗口宽度(媒体查询?)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18890053/
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
PHP conditions depending on window width (media queries?)
提问by user1706680
I have a responsive website and I need some PHP conditions depending on the windows width (or media queries).
我有一个响应式网站,我需要一些 PHP 条件,具体取决于窗口宽度(或媒体查询)。
Example:
例子:
if ($window_width > 1400px) {
echo 'Your window is wider than 1400px';
}
elseif ($window_width > 1000px) AND ($window_width < 1399px) {
echo 'Your window is between 1000px and 1399px';
}
else {
echo 'Your window is narrower than 1000px.';
}
Thanks for your help!
谢谢你的帮助!
回答by Shakti Patel
check this
检查这个
Try to use http://www.php.net/get_browserand check for isMobileDevice field. It might help only, of course, if the path to browscap.ini is set up in php.ini. If not, you can use php classes like https://github.com/garetjax/phpbrowscap
尝试使用http://www.php.net/get_browser并检查 isMobileDevice 字段。当然,如果在 php.ini 中设置了 browscap.ini 的路径,它可能会有所帮助。如果没有,你可以使用像https://github.com/garetjax/phpbrowscap这样的 php 类
回答by Janak Prajapati
Nope you can not do it with php. php is strictly server side
不,你不能用 php 做到这一点。php 是严格的服务器端
user javascript instead. Below is my code to get device resolution using javascript
用户 javascript 代替。下面是我使用 javascript 获取设备分辨率的代码
<script>
screenWidth = window.screen.width,
screenHeight = window.screen.height;
console.log(screenWidth);
console.log(screenHeight);
</script>
回答by robix
Here is the trick: Evaluate the window width with js, then load your PHP in a frame and put the width in a parameter. Your PHP script then can read that parameter and perform different conditions depending on that value.
诀窍是:使用 js 评估窗口宽度,然后将 PHP 加载到框架中并将宽度放入参数中。然后,您的 PHP 脚本可以读取该参数并根据该值执行不同的条件。
Here is the js part:
这是js部分:
<script type="text/javascript">
var width = window.innerWidth;
document.write('<iframe src="content.php?w='+width+'"></iframe>');
</script>
Within your content.php file just read the width parameter and do something with it:
在您的 content.php 文件中,只需读取 width 参数并对其进行处理:
<?php
//Get width of browser window
$width = $_GET['w'];
echo ('width: '.$width);
?>
回答by catalinp
In views you can show/hide divs, something like this:
在视图中,您可以显示/隐藏 div,如下所示:
<style>
#web {display: block;}
#mobile {display: none;}
@media screen and (max-width: 320px) {
#web {display: none;}
#mobile {display: block;}
}
</style>
<div id="mobile">
<?php echo "is mobile"; //include("page_mobile.phtml"); ?>
</div>
<div id="web">
<?php echo "is web"; //include("page_web.phtml"); ?>
</div>
回答by Jonathon
I'm assuming this is for device detection. I'm not sure if you can detect window width using PHP alone. If you can then this information would appear in the HTTP headers. I would recommend using an open source PHP class built for this: http://mobiledetect.net/
我假设这是用于设备检测。我不确定您是否可以单独使用 PHP 检测窗口宽度。如果可以,那么此信息将出现在 HTTP 标头中。我建议使用为此构建的开源 PHP 类:http: //mobiledetect.net/