How the mobile side can force the page to traverse the screen

Source: Internet
Author: User

For horizontal screen display, not vertical screen.
Experienced you must know that when the user vertical screen open, the prompt said you want to turn the phone is a very silly x thing. At this time if the user does not turn on the phone in the horizontal screen mode, but also to force users to open. This is when users have been impatient to turn off your game.
Now the solution is to write a horizontal div in portrait mode, and then turn it around.

The code is as follows:

<body class="webpBack">  <div id="print"> <p>lol</p> </div></body>

Very simple right, the ultimate ideal is to put lol in a very harmonious cross over.
Okay, here's a look at the CSS that distinguishes the horizontal screen vertical:

@Media screen and (orientation:portrait) {Html{Width: 100%;Height: 100%;padding:0px; " > white;Overflow:Hidden }Body{Width: 100%;Height: 100%;padding:0px; " > red;Overflow:Hidden }#print{Position:absolute;padding:0px; " > yellow; }}@Media screen and (Orientation:landscape) {Html{Width: 100%;Height: 100%;padding:0px; " > white; }Body{Width: 100%;Height: 100%;padding:0px; " > white; }#print{Position:absolute;Top: 0;left:  0; width:   100%; height:   100%;  padding:0px; " > yellow; }} #print p{margin: auto; margin-top:   20px; text-align: Center;}     

Plainly, is to put print this div in the vertical screen mode, horizontal screen state unchanged. So under portrait, it's not defined by its wide height. will be through the following JS to fill.

var width =Document.documentElement.clientWidth; var height =Document.documentElement.clientHeight;if (Width < height) {console.log (width + " + height); $print = $ ( ' #print '); $print.height (width); $print.css ( 2); $ ' left ', 0-(height-width)/ 2); $print.css ( ' transform ',  rotate (90deg) '); $print.css ( ' Transform-origin ',  ' 50% 50% '); } 

Here we first get the width of the available area of the screen, and then according to the relationship between the width and height to determine whether it is a horizontal screen or vertical screen. If it is a vertical screen, align and rotate the width of the print div.
The final effect is as follows:


Vertical screen
Horizontal screen

Finally, the consequence of this is that if the user's mobile phone rotation screen button is open, then when the phone cross over, it will cause a certain tragedy. The solution is as follows:

 var evt ="Onorientationchange" in window?"Orientationchange":"Resize"; Window.addeventlistener (EVT,function() {console.log (evt);var width = document.documentElement.clientWidth;var height = document.documentElement.clientHeight;$print = $ (' #print ');if (width > height) {$print. Width (width);$print. Height (height);$print. CSS (' Top ',0);$print. CSS (' Left ',0);$print. CSS (' Transform ', $print. css ( ' Transform-origin ',  ' 50% 50% '); } else{ $print. width (height);  $print. Height (width);  $print. css ( ' top ', (height-width)/ 2);  $print. css ( ' left ', 0-( Height-width)/2);  $print. css ( ' transform ',  rotate (90deg) ');  $print. css ( ' Transform-origin ',  ' 50% 50% '); }}, false);            

How the mobile side makes the page force horizontal 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.