使用meta tag "viewport"
viewport标签包括例如以下属性:
属性 | 缺省值 | 最小值 | 最大值 |
width | 980 | 200 | 10000 |
height | based on aspect ratio | 223 | 10000 |
inital-scale | fit to screen | minimum-scale | maximum-scale |
user-scalable | yes | no | yes |
minimum-scale | 0.25 | > 0 | 10 |
maximum-scale | 1.6 | >0 | 10 |
<meta name="viewport" content="width=device-width, maximum-scale=1.0" />
使用javascript脚本
以下的脚本通过检測屏幕宽度来检測方向并调整方向:
<script type="text/javascript">
var updateLayout = function() {
if (window.innerWidth != currentWidth) {
currentWidth = window.innerWidth;
var orient = (currentWidth == 320) ? "profile" : "landscape";
document.body.setAttribute("orient", orient);
window.scrollTo(0, 1);
}
};
iPhone.DomLoad(updateLayout);
setInterval(updateLayout, 400);
</script>
上述脚本可放在head部分使用CSS
使用CSS的media query:
<link rel="stylesheet" media="all and (orientation:portrait)" href="portrait.css"> <link rel="stylesheet" media="all and (orientation:landscape)" href="landscape.css">
by iefreer