Method One:
The local Window.matchmedia method allows real-time media queries.
The code is as follows:
var mql = Window.matchmedia ("(orientation:portrait)");
If there is a match, then we are in vertical view
if (mql.matches) {//Upright direction
Alert ("1")
} else {//Horizontal direction
Alert ("2")
}
Add a media query change the Listener
Mql.addlistener (function (m) {
if (m.matches) {
Change to the upright direction
Console.log ("Change to upright direction");
}
else {//change to horizontal direction
Console.log ("Change to horizontal direction");
}
});
Method Two: Monitor the adjustment size change
The code is as follows:
Window.addeventlistener ("Resize", function () {
Get screen size (internal/external width, internal/external height)
}, False);
Method Three: Use under the Jquerymobile framework
Window.addeventlistener ("Orientationchange", function () {
Values for announcing new directions
alert (window.orientation);
}, False);
Method Four:
/* Portrait */
@media screen and (orientation:portrait) {
/* portrait-specific Styles */
}
/* Landscape */
@media screen and (Orientation:landscape) {
/* landscape-specific Styles */
}
JS and CSS to detect changes in the direction of mobile devices and determine the horizontal screen