PHP if-statement screen width

偶尔善良 提交于 2019-12-25 07:18:26

问题


I want to check if the screen width is higher than 1024 pixels, to set sidebar + 50px

I tried this piece of code (but the sidebar doesnt load on 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>';

}
?>

回答1:


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).

You should use a CSS media query for this. 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. In fact your example seems to try to use jQuery syntax in PHP, which of course won't work.



来源:https://stackoverflow.com/questions/15794955/php-if-statement-screen-width

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!