在移動端中我們經常碰到橫屏豎屏的問題,那么我們應該如何去判斷或者針對橫屏、豎屏來寫不同的代碼呢。
css如何判斷橫屏豎屏
豎屏引用
<link rel="stylesheet" media="all and (orientation:portrait)" href="portrait.css">
橫屏引用
<link rel="stylesheet" media="all and (orientation:landscape)" href="landscape.css">
css代碼
@media screen and (orientation: portrait) {
/*豎屏 css*/
}
@media screen and (orientation: landscape) {
/*橫屏 css*/
}
JS判斷橫屏豎屏方法
//判斷手機橫豎屏狀態:
window.addEventListener("onorientationchange" in window ? "orientationchange" : "resize", function() {
if (window.orientation === 180 || window.orientation === 0) {
alert('豎屏狀態!');
}
if (window.orientation === 90 || window.orientation === -90 ){
alert('橫屏狀態!');
}
}, false);






