移动端字体设置rem,怎么设置

我们的手机屏幕宽度有时候不一致,字体设置一致就会展示样式有出入,对字体有兼容有两种方式

一:少部分手机适配px

 html{font-size:10px} 

 @media screen and (min-width:321px) and (max-width:375px){html{font-size:11px}} 

 @media screen and (min-width:376px) and (max-width:414px){html{font-size:12px}}

  @media screen and (min-width:415px) and (max-width:639px){html{font-size:15px}} 

  @media screen and (min-width:640px) and (max-width:719px){html{font-size:20px}} 

  @media screen and (min-width:720px) and (max-width:749px){html{font-size:22.5px}} 

  @media screen and (min-width:750px) and (max-width:799px){html{font-size:23.5px}} 

   @media screen and (min-width:800px){html{font-size:25px}}

二、使用rem对各种手机做rem的适配

rem的大小是相对与页面的根元素的大小,rem能适配各种手机

在vue的main.js中引入  import  “@/utils/js/rem.js”

      (function(d,c){

            var e=d.documentElement,

            b="orientationchange" in window?"orientationchange":"resize",

            a=function(){

                var f=e.clientWidth;

                if(!f){return}

                // if(f>750){

                //     f=640

                // }

                // e.style.fontSize=100*(f/640)+"px"

               if(f>750){

                    e.style.fontSize='100px'

                }else{

                    e.style.fontSize=100*(f/750)+"px"

                }

             };

             if(!d.addEventListener){

                 return

                }

                c.addEventListener(b,a,false);

                d.addEventListener("DOMContentLoaded",a,false)

            })(document,window);//大概跟元素的字体大小为50px  1rem=100px

 


版权声明:本文为Coco_998原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。