Gao Blog

让h5页面强制横屏

字数统计: 202阅读时长: 1 min
2018/07/25 Share

转载至:https://blog.csdn.net/qq_36457584/article/details/80967278

//注意设置*{padding:0;margin:0;} 不然有滚动条
/**
* H5强制横屏
* @param {Object}
*/
var wrapper = document.getElementById("root");
var mark = window.orientation == 90 || window.orientation == -90; //初始角度
//不同角度宽高需要互换
var width = document.documentElement[mark ? 'clientHeight' : 'clientWidth'];
var height = document.documentElement[mark ? 'clientWidth' : 'clientHeight'];
var detectOrient = function() {
var style = "";
if (window.orientation == 90 || window.orientation == -90) { //横屏
style += "width:100%;";
style += "height:100%;";
style += "-webkit-transform: rotate(0); transform: rotate(0);";
style += "-webkit-transform-origin: 0 0;";
style += "transform-origin: 0 0;";
} else if(window.orientation == 180 || window.orientation == 0){ //竖屏
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;";
}
style += "visibility: initial;";
wrapper.style.cssText = style;
$("#progress").addClass("animate-progress");
}
window.addEventListener("load", detectOrient, false);
window.addEventListener("onorientationchange" in window ? "orientationchange" : "resize", detectOrient, false);
detectOrient();
CATALOG