美文网首页
移动端适配

移动端适配

作者: INGME | 来源:发表于2023-03-15 11:50 被阅读0次
<head>
    <meta
      name="viewport"
      content="width=device-width,user-scalable=no,initial-scale=1,minimum-scale=1,maximum-scale=1,viewport-fit=cover"
    />
    <script type="text/javascript">
      // 动态设置 viewport 的 initial-scale
      var viewport = document.querySelector("meta[name=viewport]");
      var dpr = window.devicePixelRatio || 1;
      var scale = 1 / dpr;
      viewport.setAttribute(
        "content",
        "width=device-width," +
          "initial-scale=" +
          scale +
          ", maximum-scale=" +
          scale +
          ", minimum-scale=" +
          scale +
          ", user-scalable=no"
      );
      // 计算 rem font-size
      var clientWidth = 
        document.documentElement.clientWidth || document.body.clientWidth;
      clientWidth > 750 && (clientWidth = 750);
      var rootFont = (clientWidth / 7.5).toFixed(2); // 以750设计稿为例
      document.documentElement.style.fontSize = rootFont  + "px";

    </script>
  </head>

相关文章

网友评论

      本文标题:移动端适配

      本文链接:https://www.haomeiwen.com/subject/mgelrdtx.html