In HTML5, you can determine whether the landscape screen is portrait or not.

Source: Internet
Author: User

In HTML5, you can determine whether the landscape screen is portrait or not.

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

@ Media screen and (orientation: portrait) {/* landscape css */} @ media screen and (orientation: landscape) {/* landscape css */}

Write them in two CSS files separately

Portrait Screen

<link rel="stylesheet" media="all and (orientation:portrait)" href="portrait.css">

Landscape Screen

<link rel="stylesheet" media="all and (orientation:landscape)" href="landscape.css">
Ii. JS judgment on Landscape screen and landscape Screen
// Determine the screen status of the phone: 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 portrait status.

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.