zoukankan      html  css  js  c++  java
  • 移动端适配-动态计算rem

    直接以750px设计稿为例:

            (function(designWidth, maxWidth) {
                var doc = document,
                    win = window;
                var docEl = doc.documentElement;
                var remStyle = document.createElement("style");
                //获取基准字体大小
                function refreshRem() {
                    // var width = parseInt(window.screen.width); // uc有bug
                    var width = docEl.getBoundingClientRect().width;
                    if (!maxWidth) {
                        maxWidth = 750;
                    };
                    if (width > maxWidth) {
                        width = maxWidth;
                    }
                    var rem = width/designWidth*100;
                    //得到的rem基准字体大小,这里乘以100是为了适配有的浏览器不识别小数,会导致设置字体无效。
                    //设置根元素html的字体大小为基准字体大小,在css中每rem就是这个值的大小。
                    remStyle.innerHTML = 'html{font-size:' + rem + 'px;} ';
                }
                refreshRem();
                if (docEl.firstElementChild) {
                    docEl.firstElementChild.appendChild(remStyle);
                } else {
                    var wrap = doc.createElement("div");
                    wrap.appendChild(remStyle);
                    doc.write(wrap.innerHTML);
                    wrap = null;
                }
                /* 以下为在不同的时机重新计算rem*/
                win.addEventListener("resize", function() {
                    // clearTimeout(tid); //防止执行两次
                    // tid = setTimeout(refreshRem, 50);
                    refreshRem()
                }, false);
    
                win.addEventListener("pageshow", function(e) {
                    if (e.persisted) { // 浏览器后退的时候重新计算
                        refreshRem()
                        // clearTimeout(tid);
                        // tid = setTimeout(refreshRem,50);
                    }
                }, false);
            })(750, 750);

    另外找了两篇说的比较细的文章,链接如下:

    https://www.cnblogs.com/hjptopshow/p/9382448.html

    https://www.cnblogs.com/Sky-Ice/p/9596420.html

  • 相关阅读:
    hud 1166 敌兵布阵
    zznu 1914 asd的甩锅计划
    poj 1860 Currency Exchange
    poj 3026 Borg Maze (BFS + Prim)
    poj 2349 Arctic Network
    POJ 1502 MPI Maelstrom
    poj 1308 Is It A Tree?
    hdu 1272 小希的迷宫
    poj 1679 http://poj.org/problem?id=1679
    POJ 2492 A Bug's Life
  • 原文地址:https://www.cnblogs.com/zhinian-/p/12511239.html
Copyright © 2011-2022 走看看