rem 单位计算javaScript

<script>
		 (function (doc, win) {
        var docEl = doc.documentElement,
            resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize',
            recalc = function () {
                var clientWidth = docEl.clientWidth;
                if (!clientWidth) return;
                if(clientWidth>=640){
                    docEl.style.fontSize = '100px';
                }else{
                    docEl.style.fontSize = 100 * (clientWidth / 640) + 'px';
                }
            };

        if (!doc.addEventListener) return;
        win.addEventListener(resizeEvt, recalc, false);
        doc.addEventListener('DOMContentLoaded', recalc, false);
    })(document, window);
	</script>

  

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">

	<meta name=viewport content="width=device-width, initial-scale=1">
	<title>123</title>
	<style>
		*{margin:0; padding:0;}
	</style>
</head>
<body>
	<script type="text/javascript">
		var oHtml = document.getElementsByTagName('html')[0];
		var screenWidth = document.documentElement.offsetWidth || document.body.offsetWidth;
		var nowFont = (screenWidth / 640 )*625 + "%";
		oHtml.style.fontSize = nowFont;
	</script>
</body>
</html>

  

原文地址:https://www.cnblogs.com/mingjixiaohui/p/5355400.html