檢測設備橫屏 || 豎屏的狀態

JavaScript方式檢測橫屏

window.orientation:獲取屏幕旋轉的方向

window.addEventListener("resize", ()=>{
    if (window.orientation === 180 || window.orientation === 0) { 
        console.log('豎屏');
    };
    if (window.orientation === 90 || window.orientation === -90 ){ 
        console.log('橫屏');
    }  
}); 

CSS方式檢測橫屏

@media screen and (orientation: portrait) {
  /*豎屏...*/
} 
@media screen and (orientation: landscape) {
  /*橫屏...*/
}
相關文章
相關標籤/搜索