博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
react中将px转化为rem或者vw
阅读量:5832 次
发布时间:2019-06-18

本文共 4024 字,大约阅读时间需要 13 分钟。

一、px转rem

1、执行显示webpack配置

npm run eject

2、安装 postcss-px2rem-exclude、lib-flexible、sass-loader、node-sass

 

npm insatll lib-flexible sass-loader node-sass postcss-px2rem-exclude --save

 

3、修改webpack.config.js,先引入postcss-px2rem-exclude,然后在postcss-loader的plugins中加入

const px2rem = require('postcss-px2rem-exclude');
{  loader: require.resolve('postcss-loader'),  options: {    ident: 'postcss',    plugins: () => [      require('postcss-flexbugs-fixes'),      autoprefixer({        browsers: [          '>1%',          'last 4 versions',          'Firefox ESR',          'not ie < 9', // React doesn't support IE8 anyway        ],        flexbox: 'no-2009',      }),//px2rem加在这里      px2rem({        remUnit:75,exclude: /node_modules/i      })    ],  },},

4、在react入口文件引入lib-flexible

import "lib-flexible"

5、修改index.html 禁止缩放

6、完成后重启才能生效。

npm start

二、px转vw

1、安装需要的插件

npm install -D postcss-import postcss-url cssnano-preset-advanced npm install -S postcss-aspect-ratio-mini postcss-px-to-viewport postcss-write-svg postcss-cssnext cssnano postcss-viewport-units

2、index.html修改禁止缩放

3、新建配置文件 .postcssrc.js 并配置

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, // (Number) The height of the viewport.       unitPrecision: 3, // (Number) The decimal numbers to allow the REM units to grow to.       viewportUnit: 'vw', // (String) Expected units.       selectorBlackList: ['.ignore', '.hairlines'], // (Array) The selectors to ignore and leave as px.       minPixelValue: 1, // (Number) Set the minimum pixel value to replace.       mediaQuery: false // (Boolean) Allow px to be converted in media queries.    },    "postcss-viewport-units": {},    "cssnano": {      preset: "advanced",      autoprefixer: false,      "postcss-zindex": false    }  }}

4、vw的兼容处理(有些手机不支持vw单位)

npm install viewport-units-buggyfill --save

在项目入口处(如main.js)引入

let hacks = require(‘viewport-units-buggyfill.hacks‘);require(‘viewport-units-buggyfill‘).init({  hacks: hacks});

或者在index.html中引入并初始化

5、在webpack.config.js中引入并在 postcss-loader 中使用插件

const postcssAspectRatioMini = require('postcss-aspect-ratio-mini')const postcssPxToViewport = require('postcss-px-to-viewport')const postcssWriteSvg = require('postcss-write-svg')const postcssCssnext = require('postcss-cssnext')const postcssViewportUnits = require('postcss-viewport-units')const cssnano = require('cssnano')
{    loader: require.resolve('postcss-loader'),    options: {      // Necessary for external CSS imports to work      // https://github.com/facebookincubator/create-react-app/issues/2677      ident: 'postcss',      plugins: () => [        require('postcss-flexbugs-fixes'),        autoprefixer({          browsers: [            '>1%',            'last 4 versions',            'Firefox ESR',            'not ie < 9' // React doesn't support IE8 anyway          ],          flexbox: 'no-2009'        }),        postcssAspectRatioMini({}),        postcssPxToViewport({          viewportWidth: 750, // (Number) The width of the viewport.          viewportHeight: 1334, // (Number) The height of the viewport.          unitPrecision: 3, // (Number) The decimal numbers to allow the REM units to grow to.          viewportUnit: 'vw', // (String) Expected units.          selectorBlackList: ['.ignore', '.hairlines'], // (Array) The selectors to ignore and leave as px.          minPixelValue: 1, // (Number) Set the minimum pixel value to replace.          mediaQuery: false // (Boolean) Allow px to be converted in media queries.        }),        postcssWriteSvg({          utf8: false        }),        postcssCssnext({}),        postcssViewportUnits({}),        cssnano({          preset: 'advanced',          autoprefixer: false,          'postcss-zindex': false        })      ]    }  }

 

6、重启后生效

npm start

 

 

 

 

 

转载于:https://www.cnblogs.com/wanderfully/p/10832018.html

你可能感兴趣的文章
JAVA中各种日期表示字母
查看>>
[心得]关于新的挑战
查看>>
结对编程2
查看>>
python 3.6 链接mssql 进行数据操作
查看>>
颤抖吧,Css3
查看>>
Redis集群命令
查看>>
6.19心得
查看>>
西门子_TDC_数据耦合小经验
查看>>
接口测试与postman
查看>>
【转载】Nginx + Tomcat 实现反向代理
查看>>
Mac下,如何把Github上的仓库删除掉
查看>>
9.18考试 第一题count题解
查看>>
mac zsh选择到行首的快捷键
查看>>
js的apply方法使用详解,绝对NB
查看>>
linux使用crontab实现PHP执行定时任务(转)
查看>>
LINQ To XML的一些方法
查看>>
C++成员初始化顺序
查看>>
[LeetCode] Copy List with Random Pointer
查看>>
openstack部署之nova
查看>>
QNX 线程 调度策略 优先级 时钟频率 同步
查看>>