5

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

谢谢

4

4 回答 4

6

jQTouch 像这样检查它:

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

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

你也可以监听 onorientationchange 事件

请参阅上一个答案:使用 JavaScript 在浏览器中检测 Android 手机的旋转

于 2010-08-16T19:31:06.960 回答
4

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

$(window).width();

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

于 2010-08-16T17:44:32.373 回答
0
<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>
于 2015-01-08T14:26:54.340 回答
0

您可以尝试该解决方案,与所有浏览器兼容。

以下是orientationchange兼容性图: 兼容性 因此,我编写了一个orientaionchangepolyfill,它是一个基于@media 属性来修复orientationchange 的实用程序库——<a href="https://github.com/zhansingsong/orientationchange-fix" rel="nofollow noreferrer">方向更改修复

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

然后,您可以检索当前方向状态 bywindow.neworientation.current和初始方向状态 by window.neworientation.init

于 2016-09-13T09:52:43.247 回答