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