rem自适应

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

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>rem自适应</title>
    <script>
        var c = () => {
            let w = document.documentElement.clientWidth;
            let n = (20 * (w / 320) > 40 ? 40 + "px" : 20 * (w / 320) > 40 + "px");
            document.documentElement.style.fontSize = n;
        }
        window.addEventListener("load", c)
        window.addEventListener("resize", c)
    </script>
    <style>
        div {
            font-size: 1rem;
        }
    </style>
</head>

<body>
    <div>123</div>
</body>

</html>

  

posted on 2021-12-03 23:53  水行者  阅读(24)  评论(0编辑  收藏  举报

导航