其實主要是通過window.orientation實現,下面看下代碼吧
. 代碼如下:
function orient() {
if (window.orientation == 90 || window.orientation == -90) {
//ipad、iphone豎屏;Andriod橫屏
$("body").attr("class", "landscape");
orientation = 'landscape';
return false;
}
else if (window.orientation == 0 || window.orientation == 180) {
//ipad、iphone橫屏;Andriod豎屏
$("body").attr("class", "portrait");
orientation = 'portrait';
return false;
}
}
//頁面加載時調用
$(function(){
orient();
});
//用戶變化屏幕方向時調用
$(window).bind( 'orientationchange', function(e){
orient();
});
屏幕方向對應的window.orientation值:
ipad: 90 或 -90 橫屏
ipad: 0 或180 豎屏
Andriod:0 或180 橫屏
Andriod: 90 或 -90 豎屏