检测设备横屏 || 竖屏的状态

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) {
  /*横屏...*/
}

 

posted @ 2019-05-19 12:15  Model-Zachary  阅读(148)  评论(0编辑  收藏  举报