检測iPhone/iPad设备方向的三种方法

版权声明:本文为博主原创文章,未经博主同意不得转载。 https://blog.csdn.net/iefreer/article/details/24210391

使用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
为了能自己主动探測并适配到屏幕宽度。应该使用device-with而不是设定一个固定值,另外为了避免用户缩放导致界面超出屏幕,须要设置maximum-scale,

<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

猜你喜欢

转载自www.cnblogs.com/mqxnongmin/p/10500708.html