REM佈局,自適應移動端JS分享(px自動轉化為rem)
!new function(){var a=this;a.width=750,a.fontSize=100,a.widthProportion=function(){var b=(document.body&&document.body.clientWidth||document.getElementsByTagName("html")[0].offsetWidth)/a.width;return b>1?1:b},a.changePage=function(){document.getElementsByTagName("html")[0].setAttribute("style","font-size:"+a.widthProportion()*a.fontSize+"px !important")},a.changePage(),window.addEventListener("resize",function(){a.changePage()},!1)};
示例程式碼:
<!DOCTYPE html>
<html>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<head>
/*每個頁面都需要引入rem.js 程式碼如上*/
<script src="js/rem.js"></script>
<style>
/*設計圖中div的寬度為750px 高度為100px*/
div{width: 7.5rem;height: 1rem;}
</style>
</head>
<body>
<div>內容</div>
</body>
</html>