zoukankan      html  css  js  c++  java
  • rem字体响应式布局

    引用js,自动算字体大小,响应式布局

    <script>
      var iScale = 1;
      iScale = iScale / window.devicePixelRatio;
      document.write('<meta name="viewport" content="width=device-width,user-scalable=no,initial-scale=' + iScale + ',minimum-scale=' + iScale + ',maximum-scale=' + iScale + '">');

      /* 动态设置字体大小*/
      function htmlFontSizeChange() {
        var iWidth = document.documentElement.clientWidth;
        document.getElementsByTagName('html')[0].style.fontSize = iWidth / 16 + 'px';
        console.log("1rem = " + iWidth / 16);
      }
      htmlFontSizeChange();
      $(window).resize(function() {
        htmlFontSizeChange();
      });


    </script>

    <style>

      body{

        font-size:1rem; /*(16px)/(iWidth/16)  (iWidth设计图宽)*/

      }

    </style>

    示例

    <!DOCTYPE html>
      <html>

      <head>
      <meta charset="UTF-8">
      <title></title>
      <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
      <script>
        var iScale = 1;
        iScale = iScale / window.devicePixelRatio;
        document.write('<meta name="viewport" content="width=device-width,user-scalable=no,initial-scale=' + iScale + ',minimum-scale=' + iScale + ',maximum-scale=' + iScale + '">');

        /* 动态设置字体大小*/
        function htmlFontSizeChange() {
          var iWidth = document.documentElement.clientWidth;
          document.getElementsByTagName('html')[0].style.fontSize = iWidth / 16 + 'px';
          console.log("1rem = " + iWidth / 16);
        }
        htmlFontSizeChange();
        $(window).resize(function() {
          htmlFontSizeChange();
        });
      </script>
      <style>
      span {
        font-size: 0.21333333rem;
        /*设计图大小为1200,16/(1200/16)*/
        /*font-size: 16px;*/
      }

      div {
         0.4rem;
        /*30/(1200/16)*/
        /* 30px;*/
        background: red;
        height: 0.4rem;
      }
      </style>
      </head>

      <body>
        <span>设计图宽度为1200</span>
        <div></div>
      </body>

    </html>

    直接使用css设置rem

    html{

      font-size:62.5%; /* 10÷16=62.5% */

    }

    body{

      font-size:12px;

      font-size:1.2rem ; /* 12÷10=1.2 */

    }

    媒体查询

    @media only screen and (min-width: 481px){

      html {

        font-size: 94%!important;/* 15.04÷16=94% */

      }

    }

    @media only screen and (min-width: 561px){

      html {

        font-size: 109%!important; /* 17.44÷16=109% */

       }

    }

    @media only screen and (min-width: 641px){

      html {

        font-size: 125%!important;/* 20÷16=125% */

      }

    }

    引用来自 http://www.520ued.com/article/53e98eafbb16a74c41b5de77

  • 相关阅读:
    完美主义之我见
    职场-位置思维
    我的读书观
    人力资源是组织的第一战略资源-论基层员工
    积累是做成事情得唯一途径
    地理信息数据处理之我见
    word 之 插入删除空行
    OSMeteorTranslationAPI(百度,有道)对比
    CsharpOSMeteorCodeGenerator(Metero代码生成器)
    HtmlDOM 文档读取研究
  • 原文地址:https://www.cnblogs.com/kcat/p/6165067.html
Copyright © 2011-2022 走看看