rem响应式适配(postCssPxToRem + flexible.js)
在开发中经常会遇到响应式适配的问题,常用的就是通过 rem
单位来适配。这里介绍通过使用 postCssPxToRem + flexible.js
进行rem响应式适配。
移动端也可使用此方案,或使用 https://cdn.jamesy.cn/gh/JamesY-Jey/public/assets/js/rem.js 的代码。
参考文章:
vue3+vite+ts项目搭建-postcss-pxtorem
vue3.0+vite 使用 postcss-pxtorem 实现移动自适应(px转rem)
这里的环境是 Vite + Vue3
1. 安装 postCssPxToRem
1 | npm i postcss-pxtorem -D |
2. 配置
- 新建
postcss.config.js
文件
1 | module.exports = { |
- Vite环境中不生效可在
vite.config.js
中添加配置
1 | import postCssPxToRem from 'postcss-pxtorem' |
3. 引入 flexible.js
1 | npm i lib-flexible -D |
main.js
中引入
1 | import 'lib-flexible/flexible' |
- 如果要自定义适配范围使用 https://cdn.jamesy.cn/gh/JamesY-Jey/public/assets/js/flexible.js 中的代码,修改
refreshRem
函数的判断条件就可以了
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来源 哈哈哈哈密瓜 の 博客!
评论
TwikooWaline