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';