一、viewport方案

viewport指的是视口,浏览器兼容性好;
可利用vw、vh相对单位进行移动端屏幕适配

二、怎么适配

1.设置meta标签

(1)将viewport设置为设备宽度,不允许用户手动缩放
  1. <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">

(2)安装postcss-px-to-viewport插件自动将px转化为vw

a.安装插件后,修改postcss配置文件
设置设计稿跨度

(3)解决vant框架兼容问题

vant的设计稿宽度为375px,动态改变viewportWidth的值即可

  1. // postcss.config.js
  2. const path = require('path');
  3. module.exports = ({ webpack }) => {
  4. const viewWidth = webpack.resourcePath.includes(path.join('node_modules', 'vant')) ? 375 : 750;
  5. return {
  6. plugins: {
  7. autoprefixer: {},
  8. "postcss-px-to-viewport": {
  9. unitToConvert: "px",
  10. viewportWidth: viewWidth,
  11. unitPrecision: 6,
  12. propList: ["*"],
  13. viewportUnit: "vw",
  14. fontViewportUnit: "vw",
  15. selectorBlackList: [],
  16. minPixelValue: 1,
  17. mediaQuery: true,
  18. exclude: [],
  19. landscape: false
  20. }
  21. }
  22. }
  23. }