js:
var designW = 750; //设计稿宽750px
var font_rate = 100; //适配的比例,font-size为100px
document.getElementsByTagName("html")[0].style.fontSize = document.body.offsetWidth / designW * font_rate + "px";
我的rem在750px下,某个高度为232px,那么rem应该设置为2.32rem
//监测窗口大小变化
window.addEventListener("onorientationchange" in window ? "orientationchange" : "resize", function() {
document.getElementsByTagName("html")[0].style.fontSize = document.body.offsetWidth / designW * font_rate + "px";
document.getElementsByTagName("body")[0].style.fontSize = document.body.offsetWidth / designW * font_rate + "px";
}, false);
})();:
html:
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<div class='branch'>
<div class='header'>
<img src="../assets/biaoti.png" >
</div>
</div>
<style type="text/css">
*{
margin:0px;
padding:0px;
list-style: none;
}
html,body{
width:100%;
height:100%;
}
.branch{
width:100%;
height:100%;
}
.header img{
width:100%;
height:2.32rem;
}
</style>
<script src='../rem.js' type="text/javascript">
</script>
</body>
</html>