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