Detect change in orientation using javascript

后端 未结 8 1348
鱼传尺愫
鱼传尺愫 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: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:

    
    
    

    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

    
    

提交回复
热议问题