1、引入包
npm install postcss-px2rem
2、配置postcss.config.js
module.exports = {
plugins: {
'postcss-px2rem': {
remUnit: 46.875
}
}
}
3、在html文件增加脚本动态修改fontsize
<script>
; (function (win, doc) {
var docEl = doc.documentElement,
UA = navigator.userAgent;
var refreshRem = function () {
var w = docEl.getBoundingClientRect().width || 320;
var fontSize = w / 320 * 20;
fontSize = fontSize > 40 ? 40 : fontSize;
docEl.style.fontSize = fontSize + 'px';
var finalFontSize = parseFloat(win.getComputedStyle(docEl).getPropertyValue("font-size"));
if (finalFontSize === fontSize) return;
fontSize = fontSize + (fontSize - finalFontSize);
docEl.style.fontSize = fontSize + 'px';

本文介绍了如何使用postcss-px2rem工具进行移动端H5开发的适配方案,包括引入包、配置postcss.config.js以及在HTML文件中添加脚本动态调整fontsize,以适应750px设计稿。
最低0.47元/天 解锁文章
2276

被折叠的 条评论
为什么被折叠?



