CSS:
@media (orientation: portrait) { } 橫屏
@media (orientation: landscape) { }豎屏
橫屏
豎屏
JavaScript:
//判斷手機橫豎屏狀態(tài):
function hengshuping(){
if(window.orientation==180||window.orientation==0){
alert("豎屏狀態(tài)!")
}
if(window.orientation==90||window.orientation==-90){
alert("橫屏狀態(tài)!")
}
}
window.addEventListener("onorientationchange" in window ? "orientationchange" : "resize", hengshuping, false);
//移動端的瀏覽器一般都支持window.orientation這個參數(shù),通過這個參數(shù)可以判斷出手機是處在橫屏還是豎屏狀態(tài)。
從而根據(jù)實際需求而執(zhí)行相應(yīng)的程序。通過添加監(jiān)聽事件onorientationchange,進行執(zhí)行就可以了。
關(guān)于自適應(yīng),可以參考: 點擊打開鏈接