Can js/jQuery determine the orientation of the iPhone?

后端 未结 5 448
挽巷
挽巷 2020-12-04 16:31

Out of curiosity I\'ve been playing with jQuery to determine the browser\'s screen size, and it occurred to me that screen size could be used to determine whether or not a v

相关标签:
5条回答
  • 2020-12-04 16:36

    window.orientation will give you an integer that denotes the rotation. You can listen for orientation changes by adding an event to the body:

    <body onorientationchange="updateOrientation();">
    

    Just on the off-chance that the link dies or gets moved at some point:

    Value  |  Description
    -------+-------------------------------------------------------------------------------
     0     |  Portrait orientation. This is the default value.
    -90    |  Landscape orientation with the screen turned clockwise.
     90    |  Landscape orientation with the screen turned counterclockwise.
     180   |  Portrait orientation with the screen turned upside down. This value is currently not supported on iPhone.
    
    0 讨论(0)
  • 2020-12-04 16:42

    Mobile PHONES / TABLETS ONLY

    switch ( window.orientation ) {
    
        case 0:
            alert('portrait mode');
        break;
    
        case 90:
            alert('landscape mode screen turned to the left');
        break;
    
        case -90:
            alert('landscape mode screen turned to the right');
        break;
    
    }
    

    This only work for phones and tablets! Basic orientation (case 0) differs between tablets. Samsung case 0 is landscape, iPad case 0 is portrait.

    0 讨论(0)
  • 2020-12-04 16:53
    jQuery(window).bind('orientationchange', function(e) {
    
      switch ( window.orientation ) {
    
        case 0:
            alert('portrait mode');
        break;
    
        case 90:
            alert('landscape mode screen turned to the left');
        break;
    
        case -90:
            alert('landscape mode screen turned to the right');
        break;
    
      }
    
    });
    

    edit:

    While this is OK for the iPhone it may not work correctly in other devices.

    I would like to add some info I found at http://phoboslab.org/log/2012/06/x-type-making-of

    And his example is more cross browser/device compatible.

    Mobile Safari and Chrome both support the orientationchange event, which makes this easy. However, we can not rely on window.orientation, which reports the rotation in degrees (0, 90, 180 or 270), because some devices report 0° for portrait mode, while others report 0° for landscape. How convenient!

    The solution is to just check if the window height is bigger than the width – if so, we're obviously in portrait mode! But as this would be too easy, Chrome's Browser offers another challenge for us: it only updates the window dimensions after it has fired the orientationchange event. So we listen for orientationchange and resize events. Sigh.

    var wasPortrait = -1;
    var checkOrientation = function() {
        var isPortrait = (window.innerHeight > window.innerWidth);
        if( isPortrait === wasPortrait ) { return; // Nothing to do here }
        wasPortrait = isPortrait;
    
        // Do your stuff...
    };
    window.addEventListener( 'orientationchange', checkOrientation, false );
    window.addEventListener( 'resize', checkOrientation, false );
    
    0 讨论(0)
  • 2020-12-04 16:55

    See "Handling Orientation Events" of https://developer.apple.com/library/content/documentation/AppleApplications/Reference/SafariWebContent/HandlingEvents/HandlingEvents.html#//apple_ref/doc/uid/TP40006511-SW1

    0 讨论(0)
  • 2020-12-04 16:55

    I tried a similar approach for tablets cross-platform using .height() and .width() and then comparing which is larger. It works in iOS5 (tested on Ipad2) and the BlackBerry Rim but appearently not on Android (3.2 and 4.0.3). On Android the first time loading the site it gives me the right height and width but then changing the screen orientation it is always one step behind.
    Ex. using the dimensions 800x1200 starting in portrait mode:
    h:1200 w: 800 (portrait)
    h:1200 w: 800 (landscape)
    h:800 w: 1200 (portrait)
    h: 1200 w: 800 (landscape)

    Looking on I tripped over this solution with CSS3:
    How to use javascript conditionally like CSS3 media queries, orientation?
    This is probably better than tweaking the Js solution.

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