看了大漠老师的前端文章如何在Vue项目中使用vw实现移动端适配,决定在项目使用,但是发现我的脚手架是vue-cli3,和vue-cli2有点不一样,所以打算总结一下,供自己和别人参考下。
你可以参考此文档完成vw布局适配移动端,适用于vuecli3.x搭建的项目
vue create testvw
需要注意的是: 询问配置 PostCSS 时需要选择的是 In dedicated config files 在专用配置文件中
? Where do you prefer placing config for Babel, PostCSS, ESLint, etc.? (Use arrow keys)
> In dedicated config files
In package.json
具体的解释可以去查看大漠老师的如何在Vue项目中使用vw实现移动端适配
//https://github.com/michael-ciniawsky/postcss-load-config
// module.exports = {
// plugins: {
// autoprefixer: {}
// }
// }
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: 3, // 指定`px`转换为视窗单位值的小数位数(很多时候无法整除)
viewportUnit: 'vw', // 指定需要转换成的视窗单位,建议使用vw
selectorBlackList: ['.ignore', '.hairlines'], // 指定不转换为视窗单位的类,可以自定义,可以无限添加,建议定义一至两个通用的类名
minPixelValue: 1, // 小于或等于`1px`不转换为视窗单位,你也可以设置为你想要的值
mediaQuery: false // 允许在媒体查询中转换`px`
},
"postcss-viewport-units":{},
"cssnano": {
preset: "advanced",
autoprefixer: false,
"postcss-zindex": false
},
}
}
npm i cssnano postcss-aspect-ratio-mini postcss-cssnext postcss-px-to-viewport postcss-viewport-units postcss-write-svg -S
npm i postcss-import postcss-url autoprefixer cssnano-preset-advanced -D
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<link rel="icon" href="<%= BASE_URL %>favicon.ico">
<title>vuecli3vwtitle>
head>
<body>
<noscript>
<strong>We're sorry but vuecli3vw doesn't work properly without JavaScript enabled. Please enable it to continue.strong>
noscript>
<div id="app">div>
<script src="//g.alicdn.com/fdilab/lib3rd/viewport-units-buggyfill/0.6.2/??viewport-units-buggyfill.hacks.min.js,viewport-units-buggyfill.min.js">script>
<script>
window.onload = function () {
window.viewportUnitsBuggyfill.init({
hacks: window.viewportUnitsBuggyfillHacks
});
// var winDPI = window.devicePixelRatio;
// var uAgent = window.navigator.userAgent;
// var screenHeight = window.screen.height;
// var screenWidth = window.screen.width;
// var winWidth = window.innerWidth;
// var winHeight = window.innerHeight;
// alert(
// "Windows DPI:" + winDPI +
// ";\ruAgent:" + uAgent +
// ";\rScreen Width:" + screenWidth +
// ";\rScreen Height:" + screenHeight +
// ";\rWindow Width:" + winWidth +
// ";\rWindow Height:" + winHeight
// )
}
script>
body>
html>
<template>
<div id="app">
<div class='box'>
使用 vw 布局
</div>
<div id="nav">
<router-link to="/">Home</router-link> |
<router-link to="/about">About</router-link>
</div>
<router-view/>
</div>
</template>
<style>
html,body{
margin: 0;
padding: 0;
}
.box{
width: 750px;
height: 1000px;
background-color: #ccc;
color: darkred;
font-size: 40px;
}
#app {
font-family: 'Avenir', Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
}
#nav {
padding: 30px;
}
#nav a {
font-weight: bold;
color: #2c3e50;
}
#nav a.router-link-exact-active {
color: #42b983;
}
</style>
npm run serve
查看.box的css px是否成功转为vw
.box {
background-color: #ccc;
border: 1px solid #000;
color: #8b0000;
content: "viewport-units-buggyfill; width: 100vw; height: 133.333vw; font-size: 5.333vw";
font-size: 5.333vw;
height: 133.333vw;
width: 100vw;
}