H5屏幕适配

(function() {
    var doc = document;
    var docEle = doc.documentElement;
    function setRootFontSize(width) {
        width = width || docEle.getBoundingClientRect().width;
        docEle.style.fontSize = width / 375 * 10 + "px"
    }
    if (/Mobi|mljr/.test(navigator.userAgent)) { (function() {
            var timer;
            setRootFontSize();
            window.addEventListener("resize",
            function() {
                clearTimeout(timer);
                timer = setTimeout(setRootFontSize, 300)
            },
            false)
        })()
    } else { (function() {
            var pcWidth = 750;
            var style = doc.createElement("style");
            style.innerHTML = "body{width:" + pcWidth + "px!important;margin:0 auto!important}";
            doc.getElementsByTagName("head")[0].appendChild(style);
            setRootFontSize(pcWidth)
        })()
    }
})();

你可能感兴趣的:(H5屏幕适配)