web移動佈局所需:rem

Caishunqi發表於2018-10-19
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
/*所需JS*/
<script type="text/javascript">
    (function(doc, win) {
        var docEl = doc.documentElement,
                isIOS = navigator.userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/),
                dpr = isIOS ? Math.min(win.devicePixelRatio, 3) : 1,
                dpr = window.top === window.self ? dpr : 1, //被iframe引用時,禁止縮放
                dpr = 1,
                scale = 1 / dpr,
                resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize';
        docEl.dataset.dpr = dpr;
        var metaEl = doc.createElement('meta');
        metaEl.name = 'viewport';
        metaEl.content = 'initial-scale=' + scale + ',maximum-scale=' + scale + ', minimum-scale=' + scale;
        docEl.firstElementChild.appendChild(metaEl);
        var recalc = function() {
            var width = docEl.clientWidth;
            if (width / dpr > 750) {
                width = 750 * dpr;
            }
            // 乘以100,px : rem = 100 : 1
            docEl.style.fontSize = 100 * (width / 750) + 'px';
        };
        recalc()
        if (!doc.addEventListener) return;
        win.addEventListener(resizeEvt, recalc, false);
    })(document, window);
</script>

還可以使用這個:阿里團隊阿里團隊的高清方案佈局程式碼

<script>
    !function (e) {
    function t(a) {
        if (i[a])return i[a].exports;
        var n = i[a] = {exports: {}, id: a, loaded: !1};
        return e[a].call(n.exports, n, n.exports, t), n.loaded = !0, n.exports
    }

    var i = {};
    return t.m = e, t.c = i, t.p = "", t(0)
}([function (e, t) {
    "use strict";
    Object.defineProperty(t, "__esModule", {value: !0});
    var i = window;
    t["default"] = i.flex = function (normal, e, t) {
        var a = e || 100, n = t || 1, r = i.document, o = navigator.userAgent, d = o.match(/Android[\S\s]+AppleWebkit\/(\d{3})/i), l = o.match(/U3\/((\d+|\.){5,})/i), c = l && parseInt(l[1].split(".").join(""), 10) >= 80, p = navigator.appVersion.match(/(iphone|ipad|ipod)/gi), s = i.devicePixelRatio || 1;
        p || d && d[1] > 534 || c || (s = 1);
        var u = normal ? 1 : 1 / s, m = r.querySelector('meta[name="viewport"]');
        m || (m = r.createElement("meta"), m.setAttribute("name", "viewport"), r.head.appendChild(m)), m.setAttribute("content", "width=device-width,user-scalable=no,initial-scale=" + u + ",maximum-scale=" + u + ",minimum-scale=" + u), r.documentElement.style.fontSize = normal ? "50px" : a / 2 * s * n + "px"
    }, e.exports = t["default"]
}]);
flex(false, 100, 1);
</script>

全域性共用樣式

<style>
    * {
        box-sizing: border-box;
        /* 在X5新核心Blink中,在排版頁面的時候,會主動對字型進行放大,會檢測頁面中的主字型,當某一塊字型在我們的判定規則中,認為字號較小,並且是頁面中的主要字型,就會採取主動放大的操作。然而這不是我們想要的,可以採取給最大高度解決 */
        max-height: 100000px;
    }

    *:before, *:after {
        box-sizing: border-box;
        max-height: 100000px;
    }

    *, *:before, *:after {
        -webkit-tap-highlight-color: rgba(0, 0, 0, 0);
    }

    html, body, div, span, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, address, cite, code, del, dfn, em, img, ins, kbd, q, samp, small, strong, sub, sup, var, b, i, dl, dt, dd, ol, ul, li, fieldset, form, label, legend, table, caption, tbody, tfoot, thead, tr, th,textarea, td {
        border: 0 none;
        font-size: inherit;
        color: inherit;
        margin: 0;
        padding: 0;
        vertical-align: baseline;
    }

    h1, h2, h3, h4, h5, h6 {
        font-weight: normal;
    }

    em, strong {
        font-style: normal;
    }

    ul, ol, li {
        list-style: none;
    }

    body {
        font-family: "Helvetica Neue",Helvetica,"PingFang SC","Hiragino Sans GB","Microsoft YaHei","\5FAE\8F6F\96C5\9ED1",Arial,sans-serif;
        line-height: 1.5;
        color: #333;
        background-color: #f2f2f2;
        font-size: 0.24rem;
    }

    a {
        text-decoration: none;
    }
</style>

轉載自https://www.jianshu.com/p/985d26b40199

 

相關文章