【问题标题】:How to find out if ipad is in landscape/portrait mode in javascript/jquery?如何在 javascript/jquery 中确定 ipad 是否处于横向/纵向模式?
【发布时间】:2010-08-16 17:34:39
【问题描述】:

如果 ipad 处于横向模式,我想添加一个额外的 div。 是否有某种 if 语句可以找出这一点?

谢谢

【问题讨论】:

    标签: javascript jquery ipad landscape portrait


    【解决方案1】:

    jQTouch 像这样检查它:

    orientation = Math.abs(window.orientation) == 90 ? 'landscape' : 'portrait';
    

    http://github.com/senchalabs/jQTouch/blob/master/jqtouch/jqtouch.js

    你也可以监听 onorientationchange 事件

    查看上一个答案:Detect rotation of Android phone in the browser with JavaScript

    【讨论】:

      【解决方案2】:

      您可以对文档的宽度进行简单的检查。

      $(window).width();
      

      您可以将其设置为一个变量,然后根据 iPad 的原始分辨率检查该变量:纵向 768px x 1024px。

      【讨论】:

      • 太棒了!很高兴我能帮上忙。
      • 请记住,下个月将推出一款分辨率不同的新 iPad。
      • 检查 $(window).width() > $(window).height() 并且应该这样做。
      • 只是提醒一下,使用 javascript 以跨浏览器的方式检测窗口尺寸是一种痛苦:tripleodeon.com/2011/12/first-understand-your-screen
      【解决方案3】:
      <html xmlns="http://www.w3.org/1999/xhtml">
       <head>
        <title>Rotation Test</title>
        <link type="text/css" href="css/style.css" rel="stylesheet"></style>
        <script src="js/jquery-1.5.min.js" type="text/javascript"></script>
        <script type="text/javascript">
              window.addEventListener("resize", function() {
                  // Get screen size (inner/outerWidth, inner/outerHeight)
                  var height = $(window).height();
                  var width = $(window).width();
      
                  if(width>height) {
                    // Landscape
                    $("#mode").text("LANDSCAPE");
                  } else {
                    // Portrait
                    $("#mode").text("PORTRAIT");
                  }
              }, false);
      
        </script>
       </head>
       <body onorientationchange="updateOrientation();">
         <div id="mode">LANDSCAPE</div>
       </body>
      </html>
      

      【讨论】:

        【解决方案4】:

        你可以试试这个解决方案,兼容所有浏览器。

        以下是orientationchange 兼容性图片: 因此,我编写了一个orientaionchange polyfill,它是一个基于@media 属性来修复orientationchange 的实用程序库——orientationchange-fix

        window.addEventListener('orientationchange', function(){
         if(window.neworientation.current === 'portrait|landscape'){
            // do something……
         } else {
            // do something……
         }
        }, false);
        

        然后,您可以通过window.neworientation.current 检索当前方向状态,通过window.neworientation.init 检索初始方向状态。

        【讨论】:

          猜你喜欢
          • 2014-03-29
          • 2013-10-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-04-09
          • 1970-01-01
          相关资源
          最近更新 更多