How to find out if ipad is in landscape/portrait m

2020-02-26 12:39发布

I want to add an extra div if the ipad is in landscape mode. Is there some sort of if statement that could find this out?

Thanks

4条回答
做个烂人
2楼-- · 2020-02-26 12:48

jQTouch checks it like so:

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

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

You can also listen to onorientationchange events

See previous answer: Detect rotation of Android phone in the browser with JavaScript

查看更多
劫难
3楼-- · 2020-02-26 13:02
<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楼-- · 2020-02-26 13:06

You could do a simple check for the width of the document.

$(window).width();

You could set it to a variable, and then check the variable against the native resolution of the iPad: 768px x 1024px in portrait.

查看更多
老娘就宠你
5楼-- · 2020-02-26 13:11

you can try the solution, compatible with all browser.

Following is orientationchange compatibility pic: compatibility therefore, I author a orientaionchange polyfill, it is a based on @media attribute to fix orientationchange utility library——orientationchange-fix

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

and then, you can retrieve current state of orientation by window.neworientation.current and initial state of orientation by window.neworientation.init.

查看更多
登录 后发表回答