手机端自适应处理

//css

html{

font-size:10px;

}

 

元素{

font-size:1.2rem;

}

 

//js

(function (doc, win) {
    var docEl = doc.documentElement,
    resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize',
    recalc = function () {
        var clientWidth = docEl.clientWidth;
        var clientHeight = docEl.clientHeight;
        if (!clientWidth) return;
        if(window.orientation === 0 || window.orientation === 180){//判断竖屏
            docEl.style.fontSize = 10 * (clientWidth / 320) + 'px';
        }else if(window.orientation === 90 || window.orientation === -90){//判断横屏
            docEl.style.fontSize = 10 * (clientHeight / 320) + 'px';
        }
    };
    if (!doc.addEventListener) return;
    win.addEventListener(resizeEvt, recalc, false);
    doc.addEventListener('DOMContentLoaded', recalc, false);
})(document, window);

 

posted @ 2015-12-23 15:13  云端君  阅读(142)  评论(0)    收藏  举报