Background
Recently, the company is going to develop a mobile-based class web game: Long Press the button has a bicycle has been riding, hit a country's landmark will pop up the country's corresponding say hello tip, require horizontal screen display, not vertical screen.
However, when the user vertical screen opened, and did not turn on the phone in the horizontal screen mode, but also forcing users to open. This is when users have been impatient to turn off your game.
And some of the models some apps can not be horizontal screen: For example, Android has no horizontal screen mode, and iOS can turn on the horizontal screen mode.
The solution is to write a horizontal div in portrait mode, and then set the rotate positive (negative) 90 degrees, rotate him over, and if the user cuts to the horizontal screen, the rotate needs to be restored, and the requirements can be displayed normally.
Pure CSS
The main div in the vertical screen mode, the horizontal screen state is unchanged.
@media screen and (orientation: portrait) { .main { -webkit-transform:rotate(-90deg); -moz-transform: rotate(-90deg); -ms-transform: rotate(-90deg); transform: rotate(-90deg); width: 100vh; height: 100vh; /*去掉overflow 显示正常,但是浏览器有问题,竖屏时强制横屏缩小*/ overflow: hidden; }}@media screen and (orientation: landscape) { .main { -webkit-transform:rotate(0); -moz-transform: rotate(0); -ms-transform: rotate(0); transform: rotate(0) }}
But there is a problem in the horizontal screen mode, the use of CSS rotated 90 degrees, width and height is not good control.
width: 100vh;height: 100vh;
This controls the width of the height is not suitable for single wide page.
JS calculates width, alignment, rotation
As mentioned above, under the portrait, the rotation to the horizontal screen width and height will be problematic. Can be achieved by the following JS.
var width = document.documentElement.clientWidth;var height = document.documentElement.clientHeight;if( width < height ){ $print = $(‘#print‘); $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%‘);}
Note that the Transform-origin is 50% 50%, and after you rotate 90deg, you also need to reset top and left to align them.
Final Solution
If the user's phone's spin screen button is on, the code on the phone is still in question when it comes across.
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‘ , ‘none‘); $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);
Full code
/** * Screen * @param {Object} */function changeorientation ($print) {var width = document.documentElement.clientWidth; var height = document.documentElement.clientHeight; if (width < height) {$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% '); } var evt = "onorientationchange" in window? "Orientationchange": "Resize"; Window.addeventlistener (evt, function () {setTimeout (function () {var width = Document.documentElement.clien Twidth; var height = document.documentElement.clientHeight; Refreshes the width of the city initcitywidth (); Initialize the distance between each bubble and bike collision Citycrashdistancearr = Initcitycrashdistance (); if (width > height) {$print. Width (width); $print. Height (height); $print. CSS (' top ', 0); $print. CSS (' left ', 0); $print. CSS (' transform ', ' none '); $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% '); }}, 300); }, False);}
Summarize
- The scheme is only suitable for the page width and height of one screen, not suitable for rolling scenarios
- With Orientationchange and resize monitor the screen switch will have a delay problem, specifically to solve the delay scenario see my other article JS implementation of mobile phone screen events
Resources
- How the mobile side can force the page to traverse the screen
Demo
- The road of the goddess
- Ofo into 20 countries worldwide
Code
Code
https://github.com/zuopf769/notebook/blob/master/fe/%E7%A7%BB%E5%8A%A8%E7%AB%AF%E5%A6%82%E4%BD%95%E5%BC%BA%E5% 88%b6%e9%a1%b5%e9%9d%a2%e6%a8%aa%e5%b1%8f/readme.md
How the mobile side forces the page to traverse the screen