rem.js

根据设计尺寸制作rem适配
增加横屏判断

    (function(doc, win) {
        var ps_width = 375,
            ps_height = 667,
            rem = 50;
        var docEl = doc.documentElement,
            resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize',
            recalc = function(e) {
              // alert(e.type)
                var w = window.innerWidth /*docEl.clientWidth*/ ;
                var h = window.innerHeight /*docEl.clientHeight*/ ;
                document.querySelector('.fixed').style.width =w+'px';

                if (!w)
                    return;
                if (w <= h ) {
                    //if(w>ps_width){w=ps_width;}
                    docEl.style.fontSize = rem * (w / ps_width) + 'px';
                    docEl.style.width = w+'px';
                    docEl.style.margin = 'auto';
                } else {
                    var w2 = ps_width*h/ps_height;
                    docEl.style.fontSize = rem * (w2 / ps_width) + 'px';
                    docEl.style.width = w2 + 'px';
                    docEl.style.margin = '0 ' + (w - w2) / 2 + 'px';
                    document.querySelector('.fixed').style.width = w2+'px';
                }
            };
        if (!doc.addEventListener)
            return;
        win.addEventListener(resizeEvt, recalc, false);
        doc.addEventListener('DOMContentLoaded', recalc, false);
        recalc();
    })(document, window);

你可能感兴趣的:(rem.js)