In fact, it is mainly implemented through window.orientation. Let’s take a look at the code below
function orient() {
if (window. orientation == 90 || window.orientation == -90) {
//ipad, iphone vertical screen; Andriod horizontal screen
$("body").attr("class", "landscape");
orientation = 'landscape';
return false;
}
else if (window.orientation == 0 || window.orientation == 180) {
//ipad, iphone horizontal Screen; Andriod vertical screen
$("body").attr("class", "portrait");
orientation = 'portrait';
return false;
}
}
//Call
$(function(){
orient();
}) when the page is loaded;
//Call
$(window).bind when the user changes the screen orientation ('orientationchange', function(e){
orient();
});
The window.orientation value corresponding to the screen orientation:
ipad: 90 or -90 for landscape screen
ipad: 0 or 180 for portrait screen
Andriod: 0 or 180 for landscape screen
Andriod: 90 or -90 for portrait screen