• 使用rem进行页面适配


    想到写这个博客的时候,网上很多已经在吵着都2019了,还在用rem,都在用vw,emmm

    1.rem的原理

    众所周知rem的原理就是,值根据html根元素的大小而定,改变html的font-size大小便可改变元素的大小。

    2.原理大家都一样,主要在动态改变html的font-size大小时,每个人的习惯不太一样。

    网上有很多类似的博客,方法也很多

    1.使用媒体查询,类似这种

    html{font-size:10px}
    @media screen and (min-321px) and (max-375px){html{font-size:11px}}
    @media screen and (min-376px) and (max-414px){html{font-size:12px}}
    @media screen and (min-415px) and (max-639px){html{font-size:15px}}
    @media screen and (min-640px) and (max-719px){html{font-size:20px}}
    @media screen and (min-720px) and (max-749px){html{font-size:22.5px}}
    @media screen and (min-750px) and (max-799px){html{font-size:23.5px}}
    @media screen and (min-800px){html{font-size:25px}}
    

    2.借助sass,less等,类似这种

    @baseFontSize: 75;//基于视觉稿横屏尺寸/100得出的基准font-size
    .px2rem(@px){
        @return @px / @baseFontSize * 1rem;
    }
    //使用示例:
    .container {
        height: px2rem(240);
    }
    //less翻译结果:
    .container {
        height: 3.2rem;
    }  

    同时也要借助js动态设置html的font-size

    let deviceWidth = document.documentElement.clientWidth;
    document.documentElement.style.fontSize = deviceWidth / 7.5 + 'px';
      
    window.onresize = function(){
       let deviceWidth = document.documentElement.clientWidth;
       document.documentElement.style.fontSize = deviceWidth / 7.5 + 'px';
    };
    

    3.用工具,webpack,postcss,postcss-pxtorem

    postcss-pxtorem是PostCSS的插件,用于将像素单元生成rem单位。

    但配置好后,依然需要js来动态改变html的font-size

    4.借助插件https://github.com/imochen/hotcss

    不管哪种方法,所基于的原理都是一样的,最后说下自己最常用的方法

    css的calc和js

    html{
        font-size: calc(100vw / 7.5);
    }
    
    let deviceWidth = document.documentElement.clientWidth;
    document.documentElement.style.fontSize = deviceWidth / 7.5 + 'px';
      
    window.onresize = function(){
       let deviceWidth = document.documentElement.clientWidth;
       document.documentElement.style.fontSize = deviceWidth / 7.5 + 'px';
    };  

    此时设计稿大小为750px,用/100的方式可以很容易进行rem的换算,几乎不需要借助其他工具,直接设计稿大小除以100即可,并不觉得麻烦,所以一直在用  

      

      

  • 相关阅读:
    【2019.7.26 NOIP模拟赛 T1】数字查找(figure)(数学)
    【2019.7.25 NOIP模拟赛 T1】变换(change)(思维+大分类讨论)
    简析平衡树(四)——FHQ Treap
    【BZOJ3529】[SDOI2014] 数表(莫比乌斯反演)
    【洛谷1829】 [国家集训队] Crash的数字表格(重拾莫比乌斯反演)
    【PE512】Sums of totients of powers(欧拉函数)
    【CFGym102059G】Fascination Street(思维DP)
    【CF438D】The Child and Sequence(线段树)
    【2019.7.16 NOIP模拟赛 T2】折叠(fold)(动态规划)
    【UVA1303】Wall(凸包)
  • 原文地址:https://www.cnblogs.com/Anne3/p/11189015.html
Copyright © 2020-2023  润新知