Home

传传的工作甜点

A strong man will struggle with the storms of fade.

Blog About Email Github

2018-12-17
VUE使用vw实现移动端适配

一. 使用Vue-cli构建项目

通过 Vue-cli 构建的项目,在项目的根目录下有一个 .postcssrc.js,默认情况下已经有了:

1
2
3
4
5
6
7
8
module.exports = {
"plugins": {
"postcss-import": {},
"postcss-url": {},
// to edit target browsers: use "browserslist" field in package.json
"autoprefixer": {}
}
}

postcss-import

postcss-import 主要功有是解决 @import 引入路径问题。使用这个插件,可以让你很轻易的使用本地文件、node_modules 或者 web_modules 的文件。这个插件配合 postcss-url 让你引入文件变得更轻松。

postcss-url

在 Vue 项目中,vue-loader 已具有类似的功能,只需要配置中将 vue-loader 配置进去。

autoprefixer

autoprefixer 插件是用来自动处理浏览器前缀的一个插件。如果你配置了 postcss-cssnext,其中就已具备了 autoprefixer 的功能。在配置的时候,未显示的配置相关参数的话,表示使用的是 Browserslist 指定的列表参数,你也可以像这样来指定 last 2 versions 或者 > 5%。

如此一来,你在编码时不再需要考虑任何浏览器前缀的问题,可以专心撸码。这也是PostCSS最常用的一个插件之一。

二. 安装其他插件

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

三. 配置 .postcssrc.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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`
},
"cssnano": {
"cssnano-preset-advanced": {
zindex: false,
autoprefixer: false,
reduceIdents: false
},
}
}
}

特别声明:由于 cssnext 和 cssnano 都具有 autoprefixer,事实上只需要一个,所以把默认的 autoprefixer 删除掉,然后把 cssnano 中的 autoprefixer 设置为 false

postcss-cssnext

postcss-cssnext 其实就是 cssnext。该插件可以让我们使用 CSS 未来的特性,其会对这些特性做相关的兼容性处理。

cssnano

cssnano 主要用来压缩和清理 CSS 代码。在 Webpack 中,cssnano 和 css-loader 捆绑在一起,所以不需要自己加载它。不过你也可以使用 postcss-loader 显式的使用 cssnano。

==在 cssnano 的配置中,使用了 preset: “advanced”,所以我们需要另外安装:==

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

cssnano 集成了一些其他的 PostCSS 插件,如果你想禁用 cssnano 中的某个插件的时候,可以像下面这样操作:

1
2
3
4
5
6
7
"cssnano": {
"cssnano-preset-advanced": {
zindex: false,
autoprefixer: false,
reduceIdents: false
},
}

上面的代码把 autoprefixer 和 postcss-zindex 禁掉了。前者是有重复调用,后者是一个讨厌的东东。只要启用了这个插件,z-index 的值就会重置为1。这是一个天坑,==千万记得将 postcss-zindex 设置为false==。因为cssnao 压缩 css 代码 把动画名称改成了a,匹配到其他组件的动画上去了,会导致某些框架(vux)的==动画出现错误,需要将 reduceIdents 设置为 false==。

postcss-px-to-viewport

postcss-px-to-viewport 插件主要用来把px单位转换为vw、vh、vmin或者vmax这样的视窗单位,也是vw适配方案的核心插件之一。

1
2
3
4
5
6
7
8
9
10
11
"postcss-px-to-viewport": {
viewportWidth: 750, // 视窗的宽度,对应的是我们设计稿的宽度,一般是750
viewportHeight: 1334, // 视窗的高度,根据750设备的宽度来指定,一般指定1334,也可以不配置
unitPrecision: 3, // 指定`px`转换为视窗单位值的小数位数(很多时候无法整除)
viewportUnit: 'vw', // 指定需要转换成的视窗单位,建议使用vw
selectorBlackList: ['.ignore', '.hairlines'], // 指定不转换为视窗单位的类,可以自定义,可以无限添加,建议定义一至两个通用的类名
LibraryUI: ['vux'],// 使用的第三方ui库
libraryRoot: 'node_modules',// 使用的第三方ui库,目录
minPixelValue: 1, // 小于或等于`1px`不转换为视窗单位,你也可以设置为你想要的值
mediaQuery: false // 允许在媒体查询中转换`px`
}

目前出视觉设计稿,我们都是使用 750px 宽度的,那么 100vw = 750px,即 1vw = 7.5px。那么我们可以根据设计图上的 px 值直接转换成对应的 vw 值。在实际撸码过程,不需要进行任何的计算,直接在代码中写 px,比如:

1
2
3
4
5
6
7
8
9
10
.test {
border: .5px solid black;
border-bottom-width: 4px;
font-size: 14px;
line-height: 20px;
position: relative;
}
[w-188-246] {
width: 188px;
}

编译出来的CSS:

1
2
3
4
5
6
7
8
9
10
.test {
border: .5px solid #000;
border-bottom-width: .533vw;
font-size: 1.867vw;
line-height: 2.667vw;
position: relative;
}
[w-188-246] {
width: 25.067vw;
}

在不想要把 px 转换为 vw 的时候,首先在对应的元素(html)中添加配置中指定的类名 .ignore 或 .hairlines ( .hairlines一般用于设置 border-width:0.5px 的元素中):

1
<div class="box ignore"></div>

写CSS的时候:

1
2
3
4
5
6
7
8
9
10
11
.ignore {
margin: 10px;
background-color: red;
}
.box {
width: 180px;
height: 300px;
}
.hairlines {
border-bottom: 0.5px solid red;
}

编译出来的CSS:

1
2
3
4
5
6
7
8
9
10
11
.box {
width: 24vw;
height: 40vw;
}
.ignore {
margin: 10px; /*.box元素中带有.ignore类名,在这个类名写的`px`不会被转换*/
background-color: red;
}
.hairlines {
border-bottom: 0.5px solid red;
}

上面解决了px到vw的转换计算。那么在哪些地方可以使用vw来适配我们的页面。根据相关的测试:

  • 容器适配,可以使用 vw
  • 文本的适配,可以使用 vw
  • 大于 1px 的边框、圆角、阴影都可以使用 vw
  • 内距和外距,可以使用vw

postcss-aspect-ratio-mini

postcss-aspect-ratio-mini 主要用来处理元素容器宽高比。在实际使用的时候,具有一个默认的结构

1
2
3
<div aspectratio>
<div aspectratio-content></div>
</div>

在实际使用的时候,你可以把自定义属性 aspectratio 和 aspectratio-content 换成相应的类名,比如:

1
2
3
<div class="aspectratio">
<div class="aspectratio-content"></div>
</div>

结构定义之后,需要在你的样式文件中添加一个统一的宽度比默认属性:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[aspectratio] {
position: relative;
}
[aspectratio]::before {
content: '';
display: block;
width: 1px;
margin-left: -1px;
height: 0;
}
[aspectratio-content] {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
width: 100%;
height: 100%;
}

如果我们想要做一个 188:246( 188 是容器宽度,246 是容器高度)这样的比例容器,只需要这样使用:

1
2
3
[w-188-246] {
aspect-ratio: '188:246';
}

==有一点需要特别注意:aspect-ratio 属性不能和其他属性写在一起,否则编译出来的属性只会留下 aspect-ratio 的值==,比如:

1
<div aspectratio w-188-246 class="color"></div>

编译前的CSS如下:

1
2
3
4
5
[w-188-246] {
width: 188px;
background-color: red;
aspect-ratio: '188:246';
}

编译之后:

1
2
3
[w-188-246]:before {
padding-top: 130.85106382978725%;
}

主要是因为在插件中做了相应的处理,不在每次调用 aspect-ratio 时,生成前面指定的默认样式代码,这样代码没那么冗余。所以在使用的时候,需要把 width 和 background-color 分开来写:

1
2
3
4
5
6
7
[w-188-246] {
width: 188px;
background-color: red;
}
[w-188-246] {
aspect-ratio: '188:246';
}

这个时候,编译出来的 CSS 就正常了:

1
2
3
4
5
6
7
[w-188-246] {
width: 25.067vw;
background-color: red;
}
[w-188-246]:before {
padding-top: 130.85106382978725%;
}

postcss-write-svg

postcss-write-svg 插件主要用来处理移动端 1px 的解决方案。该插件主要使用的是 border-image 和 background 来做 1px 的相关处理。比如:

1
2
3
4
5
6
7
8
9
10
11
@svg 1px-border {
height: 2px;
@rect { fill: var(--color, black);
width: 100%;
height: 50%;
}
}
.example {
border: 1px solid transparent;
border-image: svg(1px-border param(--color #00b1ff)) 2 2 stretch;
}

编译出来的 CSS:

1
2
3
4
.example {
border: 1px solid transparent;
border-image: url("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http://www.w3.org/2000/svg' height='2px'%3E%3Crect fill='%2300b1ff' width='100%25' height='50%25'/%3E%3C/svg%3E") 2 2 stretch;
}

上面演示的是使用 border-image 方式,除此之外还可以使用 background-image 来实现。比如:

1
2
3
4
5
6
7
8
9
10
@svg square {
@rect {
fill: var(--color, black);
width: 100%;
height: 100%;
}
}
#example {
background: white svg(square param(--color #00b1ff));
}

编译出来就是:

1
2
3
#example {
background: white url("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http://www.w3.org/2000/svg'%3E%3Crect fill='%2300b1ff' width='100%25' height='100%25'/%3E%3C/svg%3E");
}

特别声明:由于有一些低端机对 border-image 支持度不够友好,个人建议你使用 background-image 的这个方案。


LucienWu

scribble

Blog About Email Github