Detect change in orientation using javascript

后端 未结 8 1227
鱼传尺愫
鱼传尺愫 2020-11-27 04:37

Is it possible to detect change in orientation of the browser on the iPad or Galaxy Tab using javascript? I think it\'s possible using css media queries.

相关标签:
8条回答
  • 2020-11-27 04:52

    From "Cross-device, cross-browser portrait-landscape detection"

    This is about finding out whether a mobile device is in portrait or landscape mode; you don't need to care about its orientation. For all you know, if you hold your iPad upside down, it's in portrait mode.

    $(window).bind("resize", function(){
        screenOrientation = ($(window).width() > $(window).height())? 90 : 0;
    });
    

    90 means landscape, 0 means portrait, cross browser, cross device.

    The window.onresize event is available everywhere, and it's always fired at the right time; never too early, never too late. As a matter of fact, the size of the screen is always accurate as well.

    The JavaScript version would be this, correct me please if I am wrong.

      function getScreenOrientation() {
        screenOrientation = window.outerWidth > window.outerHeight ? 90 : 0;
        console.log("screenOrientation = " + screenOrientation);
      }
      window.addEventListener("resize", function(event) {
        getScreenOrientation();
      });
      getScreenOrientation();
    
    0 讨论(0)
  • 2020-11-27 04:56

    UPDATE:

    You might want to check out

    jQuery mobile orientationchange

    or the plain JS one:

    window.addEventListener("orientationchange", function() {
      alert(window.orientation);
    }, false);
    

    MDN:

    window.addEventListener("orientationchange", function() {
        alert("the orientation of the device is now " + screen.orientation.angle);
    });
    

    Older answer

    http://www.nczonline.net/blog/2010/04/06/ipad-web-development-tips/

    Safari on the iPad does support the window.orientation property, so if necessary, you can use that to determine if the user is in horizontal or vertical mode. As reminder of this functionality:

    window.orientation is 0 when being held vertically
    window.orientation is 90 when rotated 90 degrees to the left (horizontal)
    window.orientation is -90 when rotated 90 degrees to the right (horizontal)
    

    There is also the orientationchange event that fires on the window object when the device is rotated.

    You can also use CSS media queries to determine if the iPad is being held in vertical or horizontal orientation, such as:

    <link rel="stylesheet" media="all and (orientation:portrait)" href="portrait.css">
    <link rel="stylesheet" media="all and (orientation:landscape)" href="landscape.css">
    

    http://www.htmlgoodies.com/beyond/webmaster/toolbox/article.php/3889591/Detect-and-Set-the-iPhone--iPads-Viewport-Orientation-Using-JavaScript-CSS-and-Meta-Tags.htm

    <script type="text/javascript">
    var updateLayout = function() {
      if (window.innerWidth != currentWidth) {
        currentWidth = window.innerWidth;
        var orient = (currentWidth == 320) ? "profile" : "landscape";
        document.body.setAttribute("orient", orient);
        window.scrollTo(0, 1);
      }
    };
    
    iPhone.DomLoad(updateLayout);
    setInterval(updateLayout, 400);
    </script>
    
    0 讨论(0)
  • 2020-11-27 04:56

    An easy to use snippet :

    function doOnOrientationChange()
    {
        switch(window.orientation)
        { 
            case -90:
            case 90:
              // alert('landscape');
              $('#portrait').css({display:'none'});
              $('#landscape').css({display:'block'});
    
              break;
            default:
              // alert('portrait');
              $('#portrait').css({display:'block'});
              $('#landscape').css({display:'none'});
              break;
        }
    }
    
    window.addEventListener('orientationchange', doOnOrientationChange);
    
    // First launch
    doOnOrientationChange();
    
    0 讨论(0)
  • 2020-11-27 05:07

    window.orientation is what you're looking for. there's also an onOrientationChange event works for android, iphone and, i'm mostly sure, for ipad

    0 讨论(0)
  • 2020-11-27 05:09

    I realized that nobody mentioned what happens when the device is held upside-down in this thread. window.orientation returns -90 or 90 when held horizontal. It returns 0 or 180 when held vertical. Some devices do and some don't support being held upside-down. I recommend,

    window.addEventListener("orientationchange", function() {
      
      if ( window.orientation == 0 || window.orientation == 180) {
        // WHEN IN PORTRAIT MODE
        
      } else {
        // WHEN IN LANDSCAPE MODE
        
      }
    }, false);
    

    Also note that window.orientation returns undefined on desktops.

    0 讨论(0)
  • 2020-11-27 05:12

    Adding to the @mplungjan answer, I found better results using the webkit "native" (I don't really how to called it) event, 'deviceorientation'.

    In the Mozilla Developer network they have a good explanation about how to normalize between webkit and Gecko that helped me to solve this problem.

    0 讨论(0)
提交回复
热议问题