移动端rem布局方案

动态计算html字体大小的函数

1.第一种:

 1 (function(){
 2     var html = document.documentElement;
 3     var width = html.getBoundingClientRect().width;
 4     /*  小于320px不再缩小,大于420px不再放大
 5         if(width < 320) {
 6             html.style.fontSize = 320 / 15 + "px";
 7             return
 8         }
 9         if( width > 420 ) {
10             html.style.fontSize = 420 / 15 + "px";
11             return
12         }
13     */
14         html.style.fontSize = width / 15 + "px";   // 除以多少根据设计师给的图纸宽度,尽量取整数
15 })()

2.第二种:

 1 (function(doc, win) {
 2      var docEl = doc.documentElement,
 3          resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize',
 4         recalc = function() {
 5              var clientWidth = docEl.clientWidth;
 6             if (!clientWidth) return;
 7              docEl.style.fontSize = 25 * (clientWidth / 375) + 'px';  // 375为设计师图纸宽度,25为自定义375宽度下1rem = 25px
 8          };
 9      if (!doc.addEventListener) return;
10      win.addEventListener(resizeEvt, recalc, false);
11      doc.addEventListener('DOMContentLoaded', recalc, false);
12 })(document, window);

3.第三种:自定义在设计稿宽度为640px的情况下,1rem = 100px

 1 (function() {
 2     // 根据当前屏幕的宽度和设计稿宽度的比例,动态计算一下当前宽度下的fontSize值应该
 3     // 是多少,如果fontSize的值改变了,之前设定的所有rem单位的值会跟着自动放大或者缩小
 4     var desW = 640,
 5         winW = document.documentElement.clientWidth,
 6         ratio = winW / desW;
 7         // 如果当前屏幕宽度已经大于设计稿了,为了保证图片的良好展示,我们
 8         // 一般都不让其再继续变大了,所以设计稿的宽度变为最后宽度,剩余的部分留空白展示(比如京东...)
 9         var oMain = document.getElementById("main");
10         if(winW > desW) {
11             oMain.style.width = desW + "px";
12             oMain.style.margin = "0 auto";
13         }
14         document.documentElement.style.fontSize = ratio * 100 + "px";
15 })()

4.第四种:ydui.flexible.js 是处理移动端 rem 自适应(可伸缩布局方案)的类库,无须第三方工具(如Sass/Less方法、Gulp、Sublime插件),轻松口算设计稿对应rem值

 1 /**
 2  * YDUI 可伸缩布局方案
 3  * rem计算方式:设计图尺寸px / 100 = 实际rem  例: 100px = 1rem
 4  */
 5 !function (window) {
 6 
 7     /* 设计图文档宽度 */
 8     var docWidth = 750;
 9 
10     var doc = window.document,
11         docEl = doc.documentElement,
12         resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize';
13 
14     var recalc = (function refreshRem () {
15         var clientWidth = docEl.getBoundingClientRect().width;
16 
17         /* 8.55:小于320px不再缩小,11.2:大于420px不再放大 */
18         docEl.style.fontSize = Math.max(Math.min(20 * (clientWidth / docWidth), 11.2), 8.55) * 5 + 'px';
19 
20         return refreshRem;
21     })();
22 
23     /* 添加倍屏标识,安卓为1 */
24     docEl.setAttribute('data-dpr', window.navigator.appVersion.match(/iphone/gi) ? window.devicePixelRatio : 1);
25 
26     if (/iP(hone|od|ad)/.test(window.navigator.userAgent)) {
27         /* 添加IOS标识 */
28         doc.documentElement.classList.add('ios');
29         /* IOS8以上给html添加hairline样式,以便特殊处理 */
30         if (parseInt(window.navigator.appVersion.match(/OS (d+)_(d+)_?(d+)?/)[1], 10) >= 8)
31             doc.documentElement.classList.add('hairline');
32     }
33 
34     if (!doc.addEventListener) return;
35     window.addEventListener(resizeEvt, recalc, false);
36     doc.addEventListener('DOMContentLoaded', recalc, false);
37 
38 }(window);
原文地址:https://www.cnblogs.com/tian-long/p/8421342.html