移动端rem布局JS

var docElement = document.documentElement,
    Event = "onorientationchange" in window ? "orientationchange" : "resize",
    timer;
function fn() {
    clearTimeout(timer);
    timer = setTimeout(function () {
        setRemSize(document.body.clientWidth || window.screen.availWidth || docElement.clientWidth);
    }, 50);
}

function fnInit() {
    setRemSize(document.body.clientWidth || window.screen.availWidth || docElement.clientWidth);
}

function setRemSize(size) {
    // size
    var fontSize = 100 * (size / 750) + "px";
    docElement.style.fontSize = fontSize;
    docElement.style.width = "100%";
    docElement.style.overflowX = "hidden";
}

window.addEventListener(Event, fn, false);
document.addEventListener("DOMContentLoaded", function(){
    fnInit();
}, false);

你可能感兴趣的:(移动端rem布局JS)