2

我正在为移动设备制作网站。在 android 和 windows phone 中,它正确加载了两个方向。在 iPhone 上,当您更改方向时,它不会加载新的 CSS。有什么解决办法?

解决方案:

<script type="text/javascript">  
 function orient()  
 {  
     switch(window.orientation){    
             case 0: document.getElementById("orient_css").href = "css/iphone_portrait.css";  
             break;  

             case -90: document.getElementById("orient_css").href = "css/iphone_landscape.css";  
             break;  

             case 90: document.getElementById("orient_css").href = "css/iphone_landscape.css";  
             break;  

 }  

 window.onload = orient();  

4

2 回答 2

4

您不需要 JavaScript 即可实现此功能。使用 CSS 媒体查询:

@media only screen and (orientation : portrait) {
/* Portrait styles */
}

@media only screen and (orientation : landscape) {
/* Landscape styles */
}

或者,您可以根据需要为 iPhone 横向/纵向设置特定宽度。

@media only screen and (min-width:320px) and (max-width:479px) {
/* Portrait styles */
}

@media only screen and (min-width:480px) {
/* Landscape styles */
}
于 2013-08-06T08:42:24.297 回答
1

本斯的回答是正确的方法。尽管这可能也适用于 iPad,然后您可以像这样定位 iPad

/* iPads (portrait and landscape) ----------- */
@media only screen 
and (min-device-width : 768px) 
and (max-device-width : 1024px) {
/* Styles */
}

/* iPads (landscape) ----------- */
@media only screen 
and (min-device-width : 768px) 
and (max-device-width : 1024px) 
and (orientation : landscape) {
/* Styles */
}

/* iPads (portrait) ----------- */
@media only screen 
and (min-device-width : 768px) 
and (max-device-width : 1024px) 
and (orientation : portrait) {
/* Styles */
}
于 2013-08-06T08:51:45.033 回答