Mobile phone to determine the status of the screen

Source: Internet
Author: User

Disable the user Autoscale feature: <meta name= "viewport" content= "Width=device-width, initial-scale=1.0, maximum-scale=1.0, User-scalable=0 "> Judge the state of vertical and horizontal screen There are two ways: CSS judgment, JS Judgment (a), CSS to judge the screen or portrait screen 1, written in the same CSS file
@media screen and (orientation:portrait) {  /* vertical css*/} @media screens and ( Orientation:landscape) {  /* cross-screen CSS*/}

Depending on the size of the screen, normal page size can be written in general, horizontal screen style is set separately. Just add a horizontal screen style based on the original style,

@media screen and (Orientation:landscape) {/  * horizontal css*/}
The default style is loaded when the horizontal screen style is loaded while the user is horizontal, and the horizontal screen style is removed when the screen is vertical.

2, separate write in two CSS, according to the screen to refer to different style files:

referencing different style files according to the screen

Horizontal screen:

<link rel="stylesheet" media="All and(orientation:landscape)" href="Landscape. css">

Vertical screen:

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

Note:CSS Judge screen is always seamless, that is, horizontal screen loading horizontal screen style, vertical screen loading vertical screen style,

(b) JS judge the horizontal screen or vertical screen
//determine the status of the phone screen:Window.addeventlistener ("Onorientationchange" inchWindow?"Orientationchange":"Resize", function () {if(Window.orientation = = = the|| Window.orientation = = =0) {Console.log ("Vertical Screen"); $(". codeig_s"). Removeclass ('Vercreen');//Remove a horizontal screen style        }         if(Window.orientation = = = -|| Window.orientation = = =- -) {Console.log ("Horizontal Screen"); $(". codeig_s"). AddClass ('Vercreen');//Add a horizontal screen style        }      }, false);

Note: The horizontal screen style in the Vercreen class name modifies the original default style, can be used normally, the disadvantage is that there must be a toggle state screen to trigger.

If the user by default is the horizontal screen state does not trigger the horizontal screen condition judgment, only the user from the horizontal screen to vertical screen or from the vertical screen to a horizontal screen will trigger the corresponding conditions to judge.

Without switching state will not perform any condition judgment, here is not as good as CSS to judge the overall screen.

Mobile phone to determine the status of the screen

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.