Home  >  Article  >  Web Front-end  >  Code based on jQuery to determine whether iPad, iPhone, and Android are horizontal or vertical screen_jquery

Code based on jQuery to determine whether iPad, iPhone, and Android are horizontal or vertical screen_jquery

WBOY
WBOYOriginal
2016-05-16 16:48:451956browse

In fact, it is mainly implemented through window.orientation. Let’s take a look at the code below

Copy code The code is as follows:

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

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn