rem布局

使用js 控制html的font-size

// rem.js

function recalc() { var clientWidth = document.documentElement.clientWidth; if(!clientWidth) return; var defaultWidth = 750; var dafaultFontSize = 12; var r = defaultWidth / (dafaultFontSize * 2); // 二倍 document.documentElement.style.fontSize = clientWidth / r + 'px';}(function initRecalc() { recalc(); var resizeEvt = 'osrientationchange' in window ? 'orientationchange' : 'resize'; if(!document.addEventListener) return; window.addEventListener(resizeEvt, recalc, false); document.addEventListener('DOMContentLoaded', recalc, false);})();

// rem.scss 

$defaultWidth: 750;@function pxRem($p){  @return $p*1px / 24px * 1rem;}@function pxHtml($p){  $r: $defaultWidth*1px / 24px * 1rem;  @return $p/$r*1rem*1px;}$i: 320;$deviceWidth: 320, 360, 375, 411, 414, 768, 1024;@each $width in $deviceWidth {  @media screen and (min-width: $width*1px) {    html{font-size: pxHtml($width);}  }}

你可能感兴趣的:(rem布局)