2022-5-20更新
技术栈:vue3 + webpack5
安装插件
1
| npm i postcss-px-to-viewport -D
|
增加配置
新建配置文件postcss.config.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| module.exports = () => { return { plugins: { autoprefixer: {}, "postcss-px-to-viewport": { unitToConvert: "px", viewportWidth: 750, unitPrecision: 5, propList: ["*"], viewportUnit: "vw", fontViewportUnit: "vw", selectorBlackList: [".ignore", ".hairlines", ".ig-"], minPixelValue: 1, mediaQuery: false, replace: true, include: undefined, landscape: false, landscapeUnit: "vw", landscapeWidth: 568, }, }, }; };
|
end
以下配置已经过时,请看最新内容
需要安装一下的插件
1
| npm install postcss-aspect-ratio-mini postcss-px-to-viewport postcss-write-svg postcss-cssnext postcss-viewport-units cssnano cssnano-preset-advanced postcss-import postcss-url --S
|
postcss.config.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 26 27
| module.exports = { plugins: { 'postcss-import': {}, 'postcss-url': {}, 'postcss-aspect-ratio-mini': {}, 'postcss-write-svg': { utf8: false }, 'postcss-cssnext': {}, 'postcss-px-to-viewport': { viewportWidth: 750, viewportHeight: 1334, unitPrecision: 3, viewportUnit: 'vw', selectorBlackList: ['.ignore', '.hairlines'], minPixelValue: 0, mediaQuery: false }, 'postcss-viewport-units': { filterRule: rule => rule.nodes.findIndex(i => i.prop === 'content') === -1 }, cssnano: { preset: 'advanced', autoprefixer: false, 'postcss-zindex': false } } };
|
这里注意假如生成的项目里面没有.postcssrc.js 说明写在package.json里面,记得把package里面的部分配置删除
1 2 3 4 5
| "postcss": { "plugins": { "autoprefixer": {} } },
|
最后在index.html里面进行引入viewport-units-buggyfill解决兼容问题
1 2 3 4 5 6
| <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 }); } </script>
|
注意
如果遇到图片无法正常显示
1.img图片不显示:
全局引入
1 2 3
| img { content: normal !important; }
|
2.与第三方UI库兼容问题:
使用postcss-px-to-viewport-opt,然后使用exclude配置项,具体参考 Vue+ts下的移动端vw适配(第三方库css问题)