5

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

cat
  • 1,587
  • 4
  • 15
  • 15

4 Answers4

6

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

Community
  • 1
  • 1
edtechdev
  • 686
  • 5
  • 11
4

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.

MoDFoX
  • 2,134
  • 2
  • 21
  • 22
  • Remember there is a new iPad coming out next month with a different resolution. – Karlth Feb 09 '12 at 13:34
  • 6
    check for $(window).width() > $(window).height() and that should do it. – slowBear May 15 '12 at 20:39
  • Just a reminder that detecting window dimensions with javascript in a cross-browser way is a pain : http://tripleodeon.com/2011/12/first-understand-your-screen/ – kursus Dec 16 '13 at 06:03
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>
0

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.

Zhansingsong
  • 302
  • 3
  • 11