简体   繁体   English

PHP if语句屏幕宽度

[英]PHP if-statement screen width

I want to check if the screen width is higher than 1024 pixels, to set sidebar + 50px 我想检查屏幕宽度是否大于1024像素,以设置边栏+ 50像素

I tried this piece of code (but the sidebar doesnt load on 1024+): 我尝试了这段代码(但侧栏在1024+上未加载):

<?php $arjunaOptions = arjuna_get_options(); ?>
<?php
//calculate sidebar and content area ratio
if ($arjunaOptions['sidebarDisplay'] != 'none') {
    $available = 920;
    $available2 = 970;
    $contentArea = $arjunaOptions['contentAreaWidth'];
    $sidebar = $available - $contentArea;
    $sidebarlarge = $available2 - $contentArea ;
    $sidebarLeftRight = floor(($sidebar - 50) / 2);

    print '<style type="text/css">
    @media screen and (max-width: 1024px) {
    .contentWrapper .contentArea {width:'.$contentArea.'px;}
    .contentWrapper .sidebars {width:'.$sidebar.'px;}
    .contentWrapper .sidebarLeft, .contentWrapper .sidebarRight {width:'.$sidebarLeftRight.'px;}  
    }

    @media screen and (min-width: 1025px) {
    .contentWrapper .contentArea {width:'.$contentArea.'px;}
    .contentWrapper .sidebars {width:'.$sidebarlarge.'px;}
    .contentWrapper .sidebarLeft, .contentWrapper .sidebarRight {width:'.$sidebarLeftRight.'px;}
    }

    </style>';

}
?>

You should not be doing this dynamically in PHP, since PHP has no concept of the size of the screen (without help from client side cookies or something similar). 您不应该在PHP中动态地执行此操作,因为PHP没有屏幕大小的概念(没有客户端Cookie或类似内容的帮助)。

You should use a CSS media query for this. 您应该为此使用CSS媒体查询。 For example: 例如:

@media screen and (max-width: 1024px) {
  /* CSS for up to 1024px width */   
}

@media screen and (min-width: 1025px) {
  /* CSS for over 1024px width */
}

You can also use javascript to modify the CSS properties of an element. 您还可以使用javascript修改元素的CSS属性。 In fact your example seems to try to use jQuery syntax in PHP, which of course won't work. 实际上,您的示例似乎尝试在PHP中使用jQuery语法,这当然是行不通的。

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

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