字体适配

(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 = 20 * ((clientWidth >= 640 ? 640 : clientWidth) / 320) + 'px';
        };
        recalc();
        if (!doc.addEventListener) return;
        win.addEventListener(resizeEvt, recalc, false);
        doc.addEventListener('DOMContentLoaded', recalc, false);
})(document, window);
(function (doc, win) {
        var docEl = doc.documentElement,
          isIOS = navigator.userAgent.match(/iphone|ipod|ipad/gi),
          dpr = isIOS? Math.min(win.devicePixelRatio, 3) : 1,
          dpr = window.top === window.self? dpr : 1, //被iframe引用时,禁止缩放
          dpr = 1, // 首页引用IFRAME,强制为1
          scale = 1 / dpr,
          resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize';
        docEl.dataset.dpr = win.devicePixelRatio;
        if(navigator.userAgent.match(/iphone/gi) && screen.width == 375 && win.devicePixelRatio == 2){
          docEl.classList.add('iphone6')
        }
        if(navigator.userAgent.match(/iphone/gi) && screen.width == 414 && win.devicePixelRatio == 3){
          docEl.classList.add('iphone6p')
        }
        var metaEl = doc.createElement('meta');
        metaEl.name = 'viewport';
        metaEl.content = 'initial-scale=' + scale + ',maximum-scale=' + scale + ', minimum-scale=' + scale;
        docEl.firstElementChild.appendChild(metaEl);
        var recalc = function () {
            var width = docEl.clientWidth;
            if (width / dpr > 640) {
                width = 640 * dpr;
            }
            docEl.style.fontSize = 100 * (width / 640) + 'px';
          };
        recalc()
        if (!doc.addEventListener) return;
        // win.addEventListener(resizeEvt, recalc, false);
      })(document, window);
    原文作者:代码碎片
    原文地址: https://segmentfault.com/a/1190000002660204
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞