问题
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