zoukankan      html  css  js  c++  java
  • 检測iPhone/iPad设备方向的三种方法

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

    使用meta tag "viewport"

    viewport标签包括例如以下属性:

    属性缺省值最小值最大值
    width98020010000
    heightbased on aspect ratio22310000
    inital-scalefit to screenminimum-scalemaximum-scale
    user-scalableyesnoyes
    minimum-scale0.25> 010
    maximum-scale1.6>010
    为了能自己主动探測并适配到屏幕宽度。应该使用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

  • 相关阅读:
    12
    11
    10
    9
    8
    7
    6
    5
    4
    3
  • 原文地址:https://www.cnblogs.com/mqxnongmin/p/10500708.html
Copyright © 2011-2022 走看看