移动端如何强制页面横屏

背景

最近公司要开发一个移动端的类网页游戏: 长按按钮有个自行车一直骑行,碰到某个国家的地标就弹出该国的相应say hello的tip,要求横屏显示,不能竖屏。

然而当用户竖屏打开时,而且没开启手机里的横屏模式,还要逼用户去开启。这时候用户早就不耐烦的把你的游戏关掉了。

而且有些机型有些app不能横屏:比如Android的微信就没有横屏模式,而ios的微信能开启横屏模式。

解决办法就是在竖屏模式下,写一个横屏的div,然后设置rotate正(负)90度,把他旋转过来;而且如果用户切到横屏时,需要把rotate复原,要求也能正常展现。

纯css

把main这个div在竖屏模式下横过来,横屏状态下不变。

 1 @media screen and (orientation: portrait) {
 2     .main {
 3         -webkit-transform:rotate(-90deg);
 4         -moz-transform: rotate(-90deg);
 5         -ms-transform: rotate(-90deg);
 6         transform: rotate(-90deg);
 7          100vh;
 8         height: 100vh;
 9         /*去掉overflow 微信显示正常,但是浏览器有问题,竖屏时强制横屏缩小*/
10         overflow: hidden;
11     }
12 }
1 @media screen and (orientation: landscape) {
2     .main {
3         -webkit-transform:rotate(0);
4         -moz-transform: rotate(0);
5         -ms-transform: rotate(0);
6         transform: rotate(0)
7     }
8 }
 

但是有个问题是在横屏模式下,利用css旋转90度后,宽和高不好控制。 

 100vh;
height: 100vh;

这样控制宽高不太适合单屏宽高的页面。 

js计算宽高、对齐、旋转

上文提到了,在portrait下,旋转到横屏后宽和高会有问题。可以通过下面的js来实现。 

 1 var width = document.documentElement.clientWidth;
 2 var height =  document.documentElement.clientHeight;
 3 if( width < height ){
 4   $print =  $('#print');
 5   $print.width(height);
 6   $print.height(width);
 7   $print.css('top',  (height-width)/2);
 8   $print.css('left',  0-(height-width)/2 );
 9   $print.css('transform' , 'rotate(90deg)');
10   $print.css('transform-origin' , '50% 50%');
11 }

需要注意的是transform-origin是50% 50%,旋转90deg后,还需要重新设置top和left将其对齐。

最终方案

如果用户手机的旋转屏幕按钮开着,那么当手机横过来之后,上面的代码还是有问题。

 1 var evt = "onorientationchange" in window ? "orientationchange" : "resize";
 2       
 3     window.addEventListener(evt, function() {
 4         console.log(evt);
 5         var width = document.documentElement.clientWidth;
 6          var height =  document.documentElement.clientHeight;
 7           $print =  $('#print');
 8          if( width > height ){
 9             $print.width(width);
10             $print.height(height);
11             $print.css('top',  0 );
12             $print.css('left',  0 );
13             $print.css('transform' , 'none');
14             $print.css('transform-origin' , '50% 50%');
15          }
16          else{
17             $print.width(height);
18             $print.height(width);
19             $print.css('top',  (height-width)/2 );
20             $print.css('left',  0-(height-width)/2 );
21             $print.css('transform' , 'rotate(90deg)');
22             $print.css('transform-origin' , '50% 50%');
23          }
24         
25     }, false);

办公资源网址导航 https://www.wode007.com

完整代码

 1 /**
 2  * 横竖屏
 3  * @param {Object}
 4  */
 5 function changeOrientation($print) {  
 6   var width = document.documentElement.clientWidth;
 7   var height =  document.documentElement.clientHeight;
 8   if(width < height) {
 9       $print.width(height);
10       $print.height(width);
11       $print.css('top',  (height - width) / 2 );
12       $print.css('left',  0 - (height - width) / 2 );
13       $print.css('transform', 'rotate(90deg)');
14       $print.css('transform-origin', '50% 50%');
15   } 
16   var evt = "onorientationchange" in window ? "orientationchange" : "resize";
17       window.addEventListener(evt, function() {
18       setTimeout(function() {
19           var width = document.documentElement.clientWidth;
20           var height =  document.documentElement.clientHeight;
21           // 刷新城市的宽度
22           initCityWidth();
23           // 初始化每个气泡和自行车碰撞的距离
24           cityCrashDistanceArr = initCityCrashDistance();
25     
26         if( width > height ){
27             $print.width(width);
28             $print.height(height);
29             $print.css('top',  0 );
30             $print.css('left',  0 );
31             $print.css('transform' , 'none');
32             $print.css('transform-origin' , '50% 50%');
33          }
34          else {
35           $print.width(height);
36             $print.height(width);
37             $print.css('top',  (height-width)/2 );
38             $print.css('left',  0-(height-width)/2 );
39             $print.css('transform' , 'rotate(90deg)');
40             $print.css('transform-origin' , '50% 50%');
41          }
42     }, 300);    
43    }, false);
44 }

 

原文地址:https://www.cnblogs.com/ypppt/p/13039821.html