一、基础配置
1-1 安装依赖
yarn add lib-flexible postcss-pxtorem
1-2 创建文件 postcss.config.js
module.exports = {plugins:{"postcss-pxtorem":{"rootValue":75,"propList":["*"]}}}//4.修改postcss.config.jsmodule.exports = {plugins: {//autoprefixer 自动补全css前缀的东西'autoprefixer': {//兼容的机型browsers: ['Android >= 4.0', 'iOS >= 7']},'postcss-pxtorem': {rootValue: 37.5, //换算基数,一般和html的font-size一致propList: ['*'] //哪些css属性需要换算}}};
1-3 main.js引入
import Vue from 'vue'import App from './App.vue'import "lib-flexible/flexible.js"Vue.config.productionTip = falsenew Vue({render: h => h(App),}).$mount('#app')
1-4 rem为移动端而生的要在pc和pad上跑动,一定要在App.vue文件中加
<template><div id="app"><router-view></router-view></div></template><style>#app{width:10rem;margin-left:auto;margin-right:auto;}</style>
1-5 样式重置
@符号接根路径
import '@/assets/css/reset.css'
html, body, div, span, applet, object, iframe,h1, h2, h3, h4, h5, h6, p, blockquote, pre,a, abbr, acronym, address, big, cite, code,del, dfn, em, img, ins, kbd, q, s, samp,small, strike, strong, sub, sup, tt, var,b, u, i, center,dl, dt, dd, ol, ul, li,fieldset, form, label, legend,table, caption, tbody, tfoot, thead, tr, th, td,article, aside, canvas, details, embed,figure, figcaption, footer, header, hgroup,menu, nav, output, ruby, section, summary,time, mark, audio, video {margin: 0;padding: 0;border: 0;font-size: 100%;font: inherit;vertical-align: baseline;}/* HTML5 display-role reset for older browsers */article, aside, details, figcaption, figure,footer, header, hgroup, menu, nav, section {display: block;}body {line-height: 1;}ol, ul {list-style: none;}blockquote, q {quotes: none;}blockquote:before, blockquote:after,q:before, q:after {content: '';content: none;}table {border-collapse: collapse;border-spacing: 0;}
二、Vant移动端适配
三、使用postcss-px-to-viewport
vw与vh单位,以viewport为基准,1vw与1vh分别为window.innerWidth与window.innerHeight的百分之一。- vw/vh 单位其实出现比较早了,只是以前支持性不太好,现在随着浏览器的发展,大部分(92%以上)的浏览器已经支持了vw/vh ```javascript npm i postcss-px-to-viewport -save -dev
//修改postcss.config.js
module.exports = {
plugins: {
autoprefixer: {
//兼容的机型
browsers: [‘Android >= 4.0’, ‘iOS >= 7’]
},
//px转换为vw单位的插件
“postcss-px-to-viewport”: {
//1vw = 3.2
viewportWidth: 320,
//1vh = 5.68
viewportHeight: 568,
// px to vw无法整除时,保留几位小数
unitPrecision: 5,
// 转换成vw单位
viewportUnit: ‘vw’,
//不转换的类名
selectorBlackList: [],
// 小于1px不转换
minPixelValue: 1,
//允许媒体查询中转换
mediaQuery: false,
//排除node_modules文件中第三方css文件
exclude: /(\/|\)(node_modules)(\/|\)/
},
}
};
```
