当前位置 博文首页 > 文章内容

    css rem应用

    作者: 栏目:未分类 时间:2020-10-13 15:09:55

    本站于2023年9月4日。收到“大连君*****咨询有限公司”通知
    说我们IIS7站长博客,有一篇博文用了他们的图片。
    要求我们给他们一张图片6000元。要不然法院告我们

    为避免不必要的麻烦,IIS7站长博客,全站内容图片下架、并积极应诉
    博文内容全部不再显示,请需要相关资讯的站长朋友到必应搜索。谢谢!

    另祝:版权碰瓷诈骗团伙,早日弃暗投明。

    相关新闻:借版权之名、行诈骗之实,周某因犯诈骗罪被判处有期徒刑十一年六个月

    叹!百花齐放的时代,渐行渐远!



    根据设计稿的实际宽度值,与设计稿最大宽度值,动态计算根字体大小,适应不同的屏幕比例。  

    本文的px转rem换算公式为:

    100px = 1rem 
    18px = 0.18rem

     

    基础css

    body,dl,dd,ul,ol,h1,h2,h3,h4,h5,h6,pre,form,input,textarea,p,hr,thead,tbody,tfoot,th,td{margin:0;padding:0;}        
    ul,ol{list-style:none;}
    a{text-decoration:none;}
    html{-ms-text-size-adjust:none;-webkit-text-size-adjust:none;text-size-adjust:none;}
    body{line-height:1.5; font-size:14px;}
    body,button,input,select,textarea{font-family:'helvetica neue',tahoma,'hiragino sans gb',stheiti,'wenquanyi micro hei',\5FAE\8F6F\96C5\9ED1,\5B8B\4F53,sans-serif;}
    b,strong{font-weight:bold;}
    i,em{font-style:normal;}
    table{border-collapse:collapse;border-spacing:0;}
    table th,table td{border:1px solid #ddd;padding:5px;}
    table th{font-weight:inherit;border-bottom-width:2px;border-bottom-color:#ccc;}
    img{border:0 none;width:auto\9;max-width:100%;vertical-align:top; height:auto;}
    button,input,select,textarea{font-family:inherit;font-size:100%;margin:0;vertical-align:baseline;}
    button,html input[type="button"],input[type="reset"],input[type="submit"]{-webkit-appearance:button;cursor:pointer;}
    button[disabled],input[disabled]{cursor:default;}
    input[type="checkbox"],input[type="radio"]{box-sizing:border-box;padding:0;}
    input[type="search"]{-webkit-appearance:textfield;-moz-box-sizing:content-box;-webkit-box-sizing:content-box;box-sizing:content-box;}
    input[type="search"]::-webkit-search-decoration{-webkit-appearance:none;}
    input:focus{outline:none;}
    select[size],select[multiple],select[size][multiple]{border:1px solid #AAA;padding:0;}
    article,aside,details,figcaption,figure,footer,header,hgroup,main,nav,section,summary{display:block;}
    audio,canvas,video,progress{display:inline-block;}
    body{background:#fff;}

    电脑刺绣绣花厂 http://www.szhdn.com 广州品牌设计公司https://www.houdianzi.com

    js

    脚本计算 rem单位 需要的基准字体大小  

    //designWidth:设计稿的实际宽度值,需要根据实际设置
    var DesignWidth = 375;
    //maxWidth:制作稿的最大宽度值,需要根据实际设置
    var MaxWidth = 375;
    //这段js的最后面有两个参数记得要设置,一个为设计稿实际宽度,一个为制作稿最大宽度,例如设计稿为750,最大宽度为750,则为(750,750)
    (function(designWidth, maxWidth) {
      var doc = document,
          win = window,
          docEl = doc.documentElement,
          remStyle = document.createElement("style"),
          tid;
    
      function refreshRem() {
        var width = docEl.getBoundingClientRect().width;
        maxWidth = maxWidth || 540;
        width>maxWidth && (width=maxWidth);
        var rem = width * 100 / designWidth;
        remStyle.innerHTML = 'html{font-size:' + rem + 'px;}';
      }
    
      if (docEl.firstElementChild) {
        docEl.firstElementChild.appendChild(remStyle);
      } else {
        var wrap = doc.createElement("div");
        wrap.appendChild(remStyle);
        doc.write(wrap.innerHTML);
        wrap = null;
      }
      //要等 wiewport 设置好后才能执行 refreshRem,不然 refreshRem 会执行2次;
      refreshRem();
    
      win.addEventListener("resize", function() {
        clearTimeout(tid); //防止执行两次
        tid = setTimeout(refreshRem, 300);
      }, false);
    
      win.addEventListener("pageshow", function(e) {
        if (e.persisted) { // 浏览器后退的时候重新计算
          clearTimeout(tid);
          tid = setTimeout(refreshRem, 300);
        }
      }, false);
    
      if (doc.readyState === "complete") {
        doc.body.style.fontSize = "16px";
      } else {
        doc.addEventListener("DOMContentLoaded", function(e) {
          doc.body.style.fontSize = "16px";
        }, false);
      }
    })(DesignWidth, MaxWidth);