H5移动端 rem 设置

H5移动端如果使用rem进行布局,通过js 动态设置rem

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<script type="text/javascript">
//为页面html动态设置font-size值
(function (doc, win) {
var docEl = doc.documentElement,
resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize',
recalc = function () {
var clientWidth = docEl.clientWidth;
if (!clientWidth) return;
docEl.style.fontSize = clientWidth / 7.5 + 'px';
//等价于clientWidth / 750 * 100 + 'px';
};
if (!doc.addEventListener) return;
win.addEventListener(resizeEvt, recalc, false);//resize
doc.addEventListener('DOMContentLoaded', recalc, false);//reload
})(document, window);
</script>