How to find out if ipad works in landscape / portrait mode in javascript / jquery?

I want to add an extra div if the ipad is in landscape mode. Are there any if statements that could figure this out?

thank

+5
source share
4 answers

jQTouch checks it like this:

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: Detecting Android Phone Rotation in a Browser Using JavaScript

+6
source

You can perform a simple check on the width of the document.

$(window).width();

, iPad: 768px x 1024px .

+4
<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>
0

, .

orientationchange pic: Compatibility orientaionchange polyfill, @media orientchange-- orientationchange-fix

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

window.neworientation.current window.neworientation.init.

0

All Articles