动态rem

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <style>
        html {
            font-size: 16px;
        }

        @media screen and (min-width: 375px) {
            html {
                /* iPhone6的375px尺寸作为16px基准,414px正好18px大小, 600 20px */
                font-size: calc(100% + 2 * (100vw - 375px) / 39);
                font-size: calc(16px + 2 * (100vw - 375px) / 39);
            }
        }

        @media screen and (min-width: 414px) {
            html {
                /* 414px-1000px每100像素宽字体增加1px(18px-22px) */
                font-size: calc(112.5% + 4 * (100vw - 414px) / 586);
                font-size: calc(18px + 4 * (100vw - 414px) / 586);
            }
        }

        @media screen and (min-width: 600px) {
            html {
                /* 600px-1000px每100像素宽字体增加1px(20px-24px) */
                font-size: calc(125% + 4 * (100vw - 600px) / 400);
                font-size: calc(20px + 4 * (100vw - 600px) / 400);
            }
        }

        @media screen and (min-width: 1000px) {
            html {
                /* 1000px往后是每100像素0.5px增加 */
                font-size: calc(137.5% + 6 * (100vw - 1000px) / 1000);
                font-size: calc(22px + 6 * (100vw - 1000px) / 1000);
            }
        }

        body {
            margin: 0;
            background-color: #ddd;
        }

        .picture {
            width: 300px;
            margin: 30px auto;
            box-shadow: 0px 5px 10px gray;
            padding-bottom: 10px;
            background-color: white;
            overflow: hidden;
        }

        #picture1 {
            width: 270px;
            display: block;
            margin: 0 auto;
            margin-top: 10px;
        }

        .picture > p {
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
        }

        .picture > h3, .picture > p {
            text-indent: 20px;
        }

        .picture_vw {
            width: 80vw;
            margin: 8vw auto;
            box-shadow: 0px 5px 10px gray;
            padding-bottom: 2.65vw;
            background-color: white;
            overflow: hidden;
        }

        .picture_vw #picture1 {
            width: 72vw;
            display: block;
            margin: 0 auto;
            margin-top: 2.65vw;
        }

        .picture_vw > p {
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
        }

        .picture_vw > h3, .picture_vw > p {
            text-indent: 5.33vw;
        }

        .picture_rem {
            width: 18.75rem;
            margin: 1.875rem auto;
            box-shadow: 0px 5px 10px gray;
            padding-bottom: 0.625rem;
            background-color: white;
            overflow: hidden;
        }

        .picture_rem #picture1 {
            width: 16.875rem;
            display: block;
            margin: 0 auto;
            margin-top: 0.625rem;
        }

        .picture_rem > p {
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
        }

        .picture_rem > h3, .picture_rem > p {
            text-indent: 1.25rem;
        }
    </style>
    <title>rem</title>
    <!-- 不支持vw   原设计稿750px
    <script type="text/javascript">
      !(function(doc, win) {
        var docEle = doc.documentElement, //获取html元素
          event = "onorientationchange" in window ? "orientationchange" : "resize", //判断是屏幕旋转还是resize;
          fn = function() {
            var width = docEle.clientWidth;
            width && (docEle.style.fontSize = 16 * (width / 375) + "px"); //设置html的fontSize,随着event的改变而改变。
          };
    
        win.addEventListener(event, fn, false);
        doc.addEventListener("DOMContentLoaded", fn, false);
    
      }(document, window));
    </script> 
    -->
</head>
<body>
<div class="picture">
    <img src="http://7xpvnv.com2.z0.glb.qiniucdn.com/4b0637bf-eb96-4f3e-97f3-203a9b0b1e0c" id="picture1">
    <h3>小凯</h3>
    <p>前端工程师一枚,爱生活,爱和平,爱世界,天天好心情</p>
</div>
<div class="picture_vw">
    <img src="http://7xpvnv.com2.z0.glb.qiniucdn.com/4b0637bf-eb96-4f3e-97f3-203a9b0b1e0c" id="picture1">
    <h3>小凯</h3>
    <p>前端工程师一枚,爱生活,爱和平,爱世界,天天好心情</p>
</div>
<div class="picture_rem">
    <img src="http://7xpvnv.com2.z0.glb.qiniucdn.com/4b0637bf-eb96-4f3e-97f3-203a9b0b1e0c" id="picture1">
    <h3>小凯</h3>
    <p>前端工程师一枚,爱生活,爱和平,爱世界,天天好心情</p>
</div>
</body>
</html>
!function() {
    var a = "@charset \"utf-8\";html{color:#000;background:#fff;overflow-y:scroll;-webkit-text-size-adjust:100%;-ms-text-size-adjust:100%}html *{outline:0;-webkit-text-size-adjust:none;-webkit-tap-highlight-color:rgba(0,0,0,0)}html,body{font-family:sans-serif}body,div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,code,form,fieldset,legend,input,textarea,p,blockquote,th,td,hr,button,article,aside,details,figcaption,figure,footer,header,hgroup,menu,nav,section{margin:0;padding:0}input,select,textarea{font-size:100%}table{border-collapse:collapse;border-spacing:0}fieldset,img{border:0}abbr,acronym{border:0;font-variant:normal}del{text-decoration:line-through}address,caption,cite,code,dfn,em,th,var{font-style:normal;font-weight:500}ol,ul{list-style:none}caption,th{text-align:left}h1,h2,h3,h4,h5,h6{font-size:100%;font-weight:500}q:before,q:after{content:''}sub,sup{font-size:75%;line-height:0;position:relative;vertical-align:baseline}sup{top:-.5em}sub{bottom:-.25em}a:hover{text-decoration:underline}ins,a{text-decoration:none}"
      , b = document.createElement("style");
    if (document.getElementsByTagName("head")[0].appendChild(b),
    b.styleSheet)
        b.styleSheet.disabled || (b.styleSheet.cssText = a);
    else
        try {
            b.innerHTML = a
        } catch (c) {
            b.innerText = a
        }
}();
!function(a, b) {
    function c() {
        var b = f.getBoundingClientRect().width;
        b / i > 540 && (b = 540 * i);
        var c = b / 10;
        f.style.fontSize = c + "px",
        k.rem = a.rem = c
    }
    var d, e = a.document, f = e.documentElement, g = e.querySelector('meta[name="viewport"]'), h = e.querySelector('meta[name="flexible"]'), i = 0, j = 0, k = b.flexible || (b.flexible = {});
    if (g) {
        console.warn("将根据已有的meta标签来设置缩放比例");
        var l = g.getAttribute("content").match(/initial\-scale=([\d\.]+)/);
        l && (j = parseFloat(l[1]),
        i = parseInt(1 / j))
    } else if (h) {
        var m = h.getAttribute("content");
        if (m) {
            var n = m.match(/initial\-dpr=([\d\.]+)/)
              , o = m.match(/maximum\-dpr=([\d\.]+)/);
            n && (i = parseFloat(n[1]),
            j = parseFloat((1 / i).toFixed(2))),
            o && (i = parseFloat(o[1]),
            j = parseFloat((1 / i).toFixed(2)))
        }
    }
    if (!i && !j) {
        var p = (a.navigator.appVersion.match(/android/gi),
        a.navigator.appVersion.match(/iphone/gi))
          , q = a.devicePixelRatio;
        i = p ? q >= 3 && (!i || i >= 3) ? 3 : q >= 2 && (!i || i >= 2) ? 2 : 1 : 1,
        j = 1 / i
    }
    if (f.setAttribute("data-dpr", i),
    !g)
        if (g = e.createElement("meta"),
        g.setAttribute("name", "viewport"),
        g.setAttribute("content", "initial-scale=" + j + ", maximum-scale=" + j + ", minimum-scale=" + j + ", user-scalable=no"),
        f.firstElementChild)
            f.firstElementChild.appendChild(g);
        else {
            var r = e.createElement("div");
            r.appendChild(g),
            e.write(r.innerHTML)
        }
    a.addEventListener("resize", function() {
        clearTimeout(d),
        d = setTimeout(c, 300)
    }, !1),
    a.addEventListener("pageshow", function(a) {
        a.persisted && (clearTimeout(d),
        d = setTimeout(c, 300))
    }, !1),
    "complete" === e.readyState ? e.body.style.fontSize = 12 * i + "px" : e.addEventListener("DOMContentLoaded", function() {
        e.body.style.fontSize = 12 * i + "px"
    }, !1),
    c(),
    k.dpr = a.dpr = i,
    k.refreshRem = c,
    k.rem2px = function(a) {
        var b = parseFloat(a) * this.rem;
        return "string" == typeof a && a.match(/rem$/) && (b += "px"),
        b
    }
    ,
    k.px2rem = function(a) {
        var b = parseFloat(a) / this.rem;
        return "string" == typeof a && a.match(/px$/) && (b += "rem"),
        b
    }
}(window, window.lib || (window.lib = {}));

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 这篇文章主要梳理阿里的动态rem方案。 需求: 在做手机页面的时候,经常会遇到一个问题,那就是随着智能手机的发展,...
    李悦之阅读 876评论 0 0
  • 响应式 1.meta:vp 阻止缩放 2.尽量不要写width / height,改用max / min 不要把宽...
    阿鲁提尔阅读 437评论 0 1
  • 它的特点是 不管页面多大 , 它都按照当前比例放大 相关实践 1.设置meta 禁止缩放2.设置html {fon...
    饥人谷_hak阅读 138评论 0 0
  • 动态REM是手机专用的自适应方案 REM是什么 我们常用的单位有(有问题看MDN) px em (一个汉字M的宽度...
    YM雨蒙阅读 393评论 0 0
  • 首先谈一下响应式注意点 1.meta:vp 组织缩放2.尽量不要写width/height 改用max/min3....
    冷的像风阅读 2,364评论 0 1