背景
最近公司要開發一個移動端的類網頁游戲: 長按按鈕有個自行車一直騎行,碰到某個國家的地標就彈出該國的相應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 width: 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度后,寬和高不好控制。
width: 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 }