強制橫屏 利用 CSS3 旋轉 對根容器逆時針旋轉 90 度
阿新 • • 發佈:2018-12-27
var detectOrient = function() { var width = document.documentElement.clientWidth, height = document.documentElement.clientHeight, // $wrapper = document.getElementById("J_wrapper"), $body = document.getElementsByTagName("body"), style = ""; if( width >= height ){ // 橫屏 style += "width:" + width + "px;"; // 注意旋轉後的寬高切換 style += "height:" + height + "px;"; style += "-webkit-transform: rotate(0); transform: rotate(0);"; style += "-webkit-transform-origin: 0 0;"; style += "transform-origin: 0 0;"; } else{ // 豎屏 style += "width:" + height + "px;"; style += "height:" + width + "px;"; style += "-webkit-transform: rotate(90deg); transform: rotate(90deg);"; // 注意旋轉中點的處理 style += "-webkit-transform-origin: " + width / 2 + "px " + width / 2 + "px;"; style += "transform-origin: " + width / 2 + "px " + width / 2 + "px;"; } $body[0].style.cssText = style; }; window.onresize = detectOrient; detectOrient(); //css旋轉根元素 @media screen and (orientation: portrait){ body { -webkit-transform:rotate(90deg); -webkit-transform-origin:0% 0%;/*1.重置旋轉中心*/ -moz-transform: rotate(90deg); -moz-transform-origin:0% 0%; -ms-transform: rotate(90deg); -ms-transform-origin:0% 0%; transform: rotate(90deg); transform-origin:0% 0%; width: 100vh;/*2.利用 vh 重置 ‘寬度’ */ height: 100vw;/* 3.利用 vw 重置 ‘高度’ */ top: 0; left: 100vw;/* 4.旋轉後頁面超出螢幕,重置頁面定位位置 */ } }