5
我见过很多方法来检测设备的屏幕方向,包括使用检查来测试innerWidth与innerHeight,就像这样。如何使用JavaScript检测设备方向?
function getOrientation(){
var orientation = window.innerWidth > window.innerHeight ? "Landscape" : "Primary";
return orientation;
}
但如果我要检查什么屏幕方向使用事件侦听器改变呢?我试过这段代码,但它对我不起作用。
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();
它不检测设备方向,并且侦听器没有为我注册(我正在使用Chrome的设备模拟器)。
值得注意的是,该解决方案仅适用于智能手机上的Chrome浏览器> = 39和Firefox Mobile。 OP添加了标签“ios”,Screen API不是解决方案。 – Popatop15