如果我有一个Javascript:
- 在加载时检测屏幕大小
- 根据此调整div的大小
当iPhone从纵向/横向旋转时,iPhone如何处理?它会重新加载内容吗?如果不是,如何检测它被旋转了?
如果iPhone被旋转,它不会重新加载内容。您可以通过监听window对象的resize事件来检测它是否被旋转。当屏幕大小发生变化时,该事件将被触发。
如果我有一个Javascript:
当iPhone从纵向/横向旋转时,iPhone如何处理?它会重新加载内容吗?如果不是,如何检测它被旋转了?
如果iPhone被旋转,它不会重新加载内容。您可以通过监听window对象的resize事件来检测它是否被旋转。当屏幕大小发生变化时,该事件将被触发。
<!-- for all media types(ie: screen, print etc..) and the with is no bigger then 480px load our iphone.css -->
<link rel="stylesheet" media="all and (max-device-width: 480px)" href="iphone.css">
<!-- for all media types with a minimum width of 480p, max of 1024px and orientation mode is portrait -->
<link rel="stylesheet" media="all and (min-device-width: 481px) and (max-device-width: 1024px) and (orientation:portrait)" href="ipad-portrait.css">
<!-- Same as above but load but uses the landscape style sheet -->
<link rel="stylesheet" media="all and (min-device-width: 481px) and (max-device-width: 1024px) and (orientation:landscape)" href="ipad-landscape.css">
上述管理布局的方法比JavaScript操作更快。
更具体地回答:
它会重新加载内容吗?
不,它不会重新加载内容,它显示相同的页面,因此如果您的布局例如容器宽度为width=100%,则浏览器将适应该宽度。
/*
* Media queries for responsive design
* These follow after primary styles so they will successfully override.
*/
@media all and (orientation:portrait) {
/* Style adjustments for portrait mode goes here */
}
@media all and (orientation:landscape) {
/* Style adjustments for landscape mode goes here */
}
/* Grade-A Mobile Browsers (Opera Mobile, iPhone Safari, Android Chrome)
Consider this: www.cloudfour.com/css-media-query-for-mobile-is-fools-gold/ */
@media screen and (max-device-width: 480px) {
/* Uncomment if you don't want iOS and WinMobile to mobile-optimize the text for you
j.mp/textsizeadjust
html { -webkit-text-size-adjust:none; -ms-text-size-adjust:none; } */
}
由于您的解决方案必须是对主要内容进行重新设计,因此iPhone浏览器必须重新渲染所有内容。不需要再次下载常见项目(图像、文本等),但必须重新渲染样式。
每次旋转设备时,iPhone都会重新加载文档,因此您可以执行以下操作:
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();