小编Fah*_*ikh的帖子

如何使用JavaScript检测设备方向?

我已经看到了很多方法来检测设备的屏幕方向,包括使用检查来测试innerWidth和innerHeight,就​​像这样.

function getOrientation(){
    var orientation = window.innerWidth > window.innerHeight ? "Landscape" : "Primary";
    return orientation;
}
Run Code Online (Sandbox Code Playgroud)

但是,如果我想检查屏幕方向的变化以及事件监听器怎么办?我试过这段代码,但它对我不起作用.

  function doOnOrientationChange()
  {
    switch(window.orientation) 
    {  
      case -90:
      case 90:
        alert('landscape');
        break; 
      default:
        alert('portrait');
        break; 
    }
  }

  window.addEventListener('orientationchange', doOnOrientationChange);

  // Initial execution if needed
  doOnOrientationChange();
Run Code Online (Sandbox Code Playgroud)

它不会检测设备方向,并且监听器不会为我注册(我正在使用Chrome的设备模拟器).

javascript android jqtouch cordova

7
推荐指数
1
解决办法
1万
查看次数

标签 统计

android ×1

cordova ×1

javascript ×1

jqtouch ×1