我已经看到了很多方法来检测设备的屏幕方向,包括使用检查来测试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的设备模拟器).