js
//判斷手機橫豎屏狀態:
window.addEventListener("onorientationchange" in window ? "orientationchange" : "resize", function() {
if (window.orientation === 180 || window.orientation === 0) {
//alert('豎屏狀態!');
}
if (window.orientation === 90 || window.orientation === -90 ){ // 90:右旋,-90:左旋
//alert('橫屏狀態!');
}
}, false);
//移動端的瀏覽器一般都支持window.orientation這個參數,通過這個參數可以判斷出手機是處在橫屏還是豎屏狀態。
css
@media all and (orientation : landscape) {
/*橫屏時的css*/
}
@media all and (orientation : portrait){
/*豎屏時的css*/
}