Web page to determine the phone screen or vertical screen code

Source: Internet
Author: User

HTML5 Implementation method


We make out of the H5 page in the mobile phone to browse the time, the user is likely to produce a replacement of the screen operation, then if we can determine the screen, we can better optimize our web page, and then have a better user experience. Here is the code to judge the screen:

Window.addeventlistener (' Orientationchange ', function (event) {

if (window.orientation = = 180 | | window.orientatio    n==0) {

alert ("vertical screen");

}

if (window.orientation = = | | window.orientation = -90) {

alert ("Horizontal screen");

}

});

CSS3 to judge the vertical screen of mobile phone horizontal screen

When the user spins the screen, it goes into your listening method and then window.orientation to get the status of the current screen:
0-Vertical screen
90-Rotate the horizontal screen counterclockwise
-90-clockwise rotation horizontal screen
180-vertical screen, upside down

If you do not want users to view your pages using a horizontal screen, you can keep the page vertical by monitoring the rotation of the body using the CSS3 inside the transition during the device rotation.

A page on a mobile device that has a Orientationchange event when the screen rotates. You can increase the monitoring of this event for the BODY element:

<body onorientationchange= "updateorientation ();" >
Monitor changes in the form to monitor the screen of the phone @media screens and
(orientation:portrait) {
css[vertical definition style]
}
@media Screen and (Orientation:landscape) {
css[horizontal definition style]
}

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.