H5開發(fā):橫屏適配

今天一個同事讓幫忙改動一個h5,直接打開h5就進入橫屏模式,研究了下,有個騷操作,廢話不多說直接上代碼

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        #content {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
        }

        #myiframe{
            position: absolute;
            left: 0;
            top: 0;
            border: none;
            width: 100% !important;
            height: 100% !important;
            z-index: 1800;
        }
    </style>
</head>

<body>

    <div id="content">
        <iframe src="http://www.baidu.com" id="myiframe" scrolling="no" onload="changeFrameHeight()" frameborder="0"></iframe>
    </div>
    <script>
        function changeFrameHeight() {
            var ifm = document.getElementById("myiframe");
            ifm.height = document.documentElement.clientHeight;

        }

        window.onresize = function () {
            changeFrameHeight();

        }
        // 利用 CSS3 旋轉(zhuǎn) 對根容器逆時針旋轉(zhuǎn) 90 度
        var detectOrient = function () {
            var width = document.documentElement.clientWidth,
                height = document.documentElement.clientHeight,
                $wrapper = document.getElementById("content"),
                style = "";
            if (width >= height) { // 橫屏
                style += "width:" + width + "px;";  // 注意旋轉(zhuǎn)后的寬高切換
                style += "height:" + height + "px;";
                style += "-webkit-transform: rotate(0); transform: rotate(0);";
                style += "-webkit-transform-origin: 0 0;";
                style += "transform-origin: 0 0;";
                console.log(1)
            }
            else { // 豎屏
                style += "width:" + height + "px;";
                style += "height:" + width + "px;";
                style += "-webkit-transform: rotate(90deg); transform: rotate(90deg);";
                // 注意旋轉(zhuǎn)中點的處理
                style += "-webkit-transform-origin: " + width / 2 + "px " + width / 2 + "px;";
                style += "transform-origin: " + width / 2 + "px " + width / 2 + "px;";
                console.log(2)
            }
            $wrapper.style.cssText = style;
        }
        window.onresize = detectOrient;
        detectOrient();
    </script>
</body>

</html>

效果圖

正常橫屏顯示

image.png

豎屏顯示橫屏樣式


image.png

總結(jié):使用這種可以輕易的實現(xiàn)橫屏適配,調(diào)試的頁面較少,以上可能還有其他的bug

參考:
https://zhuanlan.zhihu.com/p/30577906

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,030評論 25 709
  • 1、通過CocoaPods安裝項目名稱項目信息 AFNetworking網(wǎng)絡(luò)請求組件 FMDB本地數(shù)據(jù)庫組件 SD...
    陽明AI閱讀 16,203評論 3 119
  • 初拿毛筆,未細思,只是隨意起筆,今日看,依舊難以啟及,當初的心意,今日的執(zhí)迷。 無法到達,就好好懷念,也好。
    丹青筆憶閱讀 202評論 1 3
  • 看好,新店開業(yè)生意不好怎么辦。首先就是開店前需要準備什么? 1、通過調(diào)查了解你的目標顧客群的喜好,他們喜好...
    DarrenAnn閱讀 3,580評論 4 1

友情鏈接更多精彩內(nèi)容