1. 程式人生 > >讓h5頁面強制橫屏

讓h5頁面強制橫屏

var detectOrient = function() {
        var width = document.documentElement.clientWidth,
                height = document.documentElement.clientHeight,
                wrapper = document.getElementById("main"),
                style = "";
        if(width >= height) { // 豎屏
            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 { // 橫屏
            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;";
        }
        wrapper.style.cssText = style;
    }
    window.onresize = detectOrient;
    detectOrient();