In HTML5, the method for determining the portrait of a landscape screen (mobile terminal) is html5.
We often encounter horizontal and vertical screen problems on mobile terminals. How should we judge or write different codes for horizontal and vertical screens.
There are two methods:
I. CSS determines whether a landscape screen is portrait
Written in the same CSS
Copy XML/HTML Code to clipboard
- @ Media screen and (orientation: portrait ){
- /* Landscape css */
- }
- @ Media screen and (orientation: landscape ){
- /* Landscape css */
- }
Write them in two CSS files separately
Portrait Screen
Copy XML/HTML Code to clipboard
- <Link rel = "stylesheet" media = "all and (orientation: portrait)" href = "portrait.css">
Landscape Screen
Copy XML/HTML Code to clipboard
- <Link rel = "stylesheet" media = "all and (orientation: landscape)" href = "landscape.css">
Ii. JS judgment on Landscape screen and landscape Screen
Copy XML/HTML Code to clipboard
- // Determine the mobile phone portrait status:
- Window. addEventListener ("onorientationchange" in window? "Orientationchange": "resize", function (){
- If (window. orientation = 180 | window. orientation = 0 ){
- Alert ('portrait status! ');
- }
- If (window. orientation = 90 | window. orientation =-90 ){
- Alert ('landscape status! ');
- }
- }, False );
// Mobile browsers generally support the window. orientation parameter. You can use this parameter to determine whether the mobile phone is in the landscape or landscape.
Window. orientation value corresponding to the screen direction:
Ipad, iphone: 90 or-90 horizontal screen
Ipad, iphone: 0 or 180 portrait Screen
Andriod: 0 or 180 horizontal screen
Andriod: 90 or-90 portrait Screen