In HTML5, the method for determining the portrait of a landscape screen (mobile terminal) is html5.

Source: Internet
Author: User

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
  1. @ Media screen and (orientation: portrait ){
  2. /* Landscape css */
  3. }
  4. @ Media screen and (orientation: landscape ){
  5. /* Landscape css */
  6. }

Write them in two CSS files separately

Portrait Screen

Copy XML/HTML Code to clipboard
  1. <Link rel = "stylesheet" media = "all and (orientation: portrait)" href = "portrait.css">

Landscape Screen

Copy XML/HTML Code to clipboard
  1. <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
  1. // Determine the mobile phone portrait status:
  2. Window. addEventListener ("onorientationchange" in window? "Orientationchange": "resize", function (){
  3. If (window. orientation = 180 | window. orientation = 0 ){
  4. Alert ('portrait status! ');
  5. }
  6. If (window. orientation = 90 | window. orientation =-90 ){
  7. Alert ('landscape status! ');
  8. }
  9. }, 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.