Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/442.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Android中的mediaMatch?(电话间隙定向)_Javascript_Android_Cordova - Fatal编程技术网

Javascript Android中的mediaMatch?(电话间隙定向)

Javascript Android中的mediaMatch?(电话间隙定向),javascript,android,cordova,Javascript,Android,Cordova,我正在努力改变Phonegap中的方向。我的计划是在方向改变时改变CSS文件(不管怎样,这可能是一种复杂的方式),但我正在努力让任何依赖方向的事件发生。我目前正在使用以下代码: window.onorientationchange = function() { navigator.notification.alert( 'orientation change!', // message 'Portrait', // title

我正在努力改变Phonegap中的方向。我的计划是在方向改变时改变CSS文件(不管怎样,这可能是一种复杂的方式),但我正在努力让任何依赖方向的事件发生。我目前正在使用以下代码:

window.onorientationchange = function() {

  navigator.notification.alert(
        'orientation change!',  // message
        'Portrait',            // title
        'buttton');              // buttonName

  if (window.matchMedia("(orientation: portrait)").matches){
      navigator.notification.alert(
        'now in portrait',  // message
        'Portrait',            // title
        'buttton');              // buttonName
  }
  else{
    alert("landscape")
  }
}

第一个警报(当方向改变时)很好,但第二个警报(当它具体改变为横向时)没有发生。我使用的是安卓2.3.5版。(这个方法是由Razvan提出的)

您需要在MediaQueryList对象上设置一个侦听器。例如,见下文

除了媒体查询之外,还有其他方法可以检索设备方向更改。搜索“mdn方向更改”以获取示例

  • 创建MediaQueryList对象“方向”。包含添加/删除列表、媒体和匹配项
  • 创建侦听器“定向侦听器”。媒体查询匹配的测试
  • 获取初始方向值
  • 倾听是否会发生任何变化

    var orientation = window.matchMedia("(orientation: portrait)"),
        orientationListener = function(mql) {
    
            if (mql.matches) {
                // Media query does match orientation portrait
            } else {
                // Media query does not match which should mean orientation is now landscape
            }
    
        };
    
    // Gets initial match
    
    orientationListener(orientation);
    
    // Listens for change
    
    orientation.addListener(orientationListener);