- 在head 内加入js代码,根据浏览器宽度设置根元素字体大小,以下代码为设计稿1920的转换,为了计算方法,1rem = 1920 / 100 = 196 px ,写css 时根据设计稿标注除以100即可
<script>
var deviceWidth
setHtmlFontSize()
if (window.addEventListener) {
window.addEventListener('resize', function () {
setHtmlFontSize()
}, false)
}
function setHtmlFontSize() {
// 1366是设计稿的宽度,当大于1366时采用1366宽度,比例也是除以13.66
deviceWidth = document.documentElement.clientWidth > 1920 ? 1920 : document.documentElement.clientWidth
var rootFontSize = deviceWidth / 19.2
if (rootFontSize < 66) {
rootFontSize = 66
}
document.getElementsByTagName('html')[0].style.cssText = 'font-size:' + rootFontSize + 'px !important'
}
</script>
2.vscode 插件 cssrem 在设置中将rootfontsize设置为196,重启vscode ,写css 时按设计稿标注除以100即可