如何使用javascript或Jquery更改屏幕方向?

til*_*lak 2 javascript jquery cordova

我正在使用Phonegap for Android和iphone开发应用程序.当我从一个页面导航到另一个页面时,我需要更改屏幕方向.任何人都可以告诉你如何通过java脚本或jquery完成它?

谢谢

the*_*dox 5

你可以试试这个:

$(window).resize(function() {
    var height = $(window).height();
    var width = $(window).width();

    if (width > height) {
        // Landscape
        $("#mode").text("LANDSCAPE");
    } else {
        // Portrait
        $("#mode").text("PORTRAIT");
    }

});
Run Code Online (Sandbox Code Playgroud)

另一个window.orientation返回的id 0,90或者-90:

  • 0 代表:肖像模式

  • 90 代表:横向模式,屏幕向左转

  • -90 代表:横向模式,屏幕向右转

     window.onorientationchange = function() 
    {
    var orientation = window.orientation;
    switch(orientation) {
        case 0:
            //Top side up.
            break; 
    
        case -90:
            //Left side up (turned 90 degrees to the right)
            break;
    
        case 90: 
            //Right side up (turned 90 degrees to the left)
            break;
      }
    }
    
    Run Code Online (Sandbox Code Playgroud)

你也可以看到

使用JavaScript查看Android手机在浏览器中的旋转信息.