代码拉取完成,页面将自动刷新
vw
布局适配移动端,适用于vuecli3.x
搭建的项目vuecli3.x
搭建项目.postcssrc.js
文件中增加配置项npm
下载相关包index.html
引入js插件并使用该插件App.vue
组件中尝试使用vw布局vuecli3.x
创建项目vue create vuecli3vw
需要注意的是: 询问配置 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
.postcssrc.js
文件中增加配置项.postcssrc.js
全部内容//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
下载相关包npm i cssnano postcss-aspect-ratio-mini postcss-cssnext postcss-px-to-viewport postcss-viewport-units postcss-write-svg -S
npm i cssnano-preset-advanced -D
npm i postcss-import postcss-url autoprefixer -D
index.html
引入js插件并使用该插件<!DOCTYPE html>
<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>vuecli3vw</title>
</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>
<!-- built files will be auto injected -->
<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>
App.vue
组件中尝试使用vw布局<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
vw
,恭喜你成功啦!npm install
npm run serve
npm run build
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。