vue中移动端页面的适配使用rem或者vw换算:

1,068 阅读2分钟

1、根据rem换算的步骤:在APP.vue里写入以下代码:

   根元素样式要改为:

html {font-size: 625%; /*100 ÷ 16 × 100% = 625%*/}

再用总结得出的各分辨率媒体查询换算:

@media screen and (min-width:360px) and (max-width:374px) and (orientation:portrait) {
    html { font-size: 703%; }
}
@media screen and (min-width:375px) and (max-width:383px) and (orientation:portrait) {
    html { font-size: 732.4%; }
}
@media screen and (min-width:384px) and (max-width:399px) and (orientation:portrait) {
    html { font-size: 750%; }
}
@media screen and (min-width:400px) and (max-width:413px) and (orientation:portrait) {
    html { font-size: 781.25%; }
}
@media screen and (min-width:414px) and (max-width:431px) and (orientation:portrait){
    html { font-size: 808.6%; }
}
@media screen and (min-width:432px) and (max-width:479px) and (orientation:portrait){
    html { font-size: 843.75%; }
}

2、使用VM换算的步骤:

  1、安装:postcss-import  、postcss-url

 npm i postcss-import postcss-url --save-dev

 2、安装:postcss-aspect-ratio-mini 、postcss-px-to-viewport、postcss-write-svg、postcss-cssnext、cssnano

npm i postcss-aspect-ratio-mini postcss-px-to-viewport postcss-write-svg postcss-cssnext cssnano --S

 3、安装:cssnano-preset-advanced

npm i cssnano-preset-advanced --save-dev

4、配置.postcssrc.js文件:

module.exports = {
 "plugins": {   
 "postcss-import": {}, 
   "postcss-url": {}, 
   "postcss-aspect-ratio-mini": {}, 
   "postcss-write-svg": {    
  utf8: false  
  },  
  "postcss-cssnext": {}, 
   "postcss-px-to-viewport": {    
   "viewportWidth": 750, // 视窗的宽度,对应的是我们设计稿的宽度,一般是750      
   "viewportHeight": 1334, // 视窗的高度,根据750设备的宽度来指定,一般指定1334     
    "unitPrecision": 4, // 指定`px`转换为视窗单位值的小数位数(很多时候无法整除)    
    "viewportUnit": 'vw', // 指定需要转换成的视窗单位,建议使用vw   
    "selectorBlackList": ['.ignore', '.hairlines'], // 指定不转换为视窗单位的类,可以自定义,可以无限添加,建议定义一至两个通用的类名  
    "minPixelValue": 1, // 小于或等于`1px`不转换为视窗单位,你也可以设置为你想要的值     
    "mediaQuery": true // 允许在媒体查询中转换`px`    },   
 "cssnano": {    
   "preset": "advanced",   
    "autoprefixer": false, // cssnext也具备autoprefixer功能,所以把cssnano的设置为false  
    "postcss-zindex": false , // 避免 cssnano 重新计算 z-index   
 }  }}

5、已完成,使用rem的话是除以100的比例,使用VW直接使用px会自动转换成VW,VW转换行内样式不生效!