(function (designwidth, maxwidth) { var doc = document, win = window; var docel = doc.documentelement; var tid; var rootitem, rootstyle; function refreshrem() { var width = docel.getboundingclientrect().width; if (!maxwidth) { maxwidth = 540; } ; if (width > maxwidth) { width = maxwidth; } //与淘宝做法不同,直接采用简单的rem换算方法1rem=100px //var rem = width * 100 / designwidth; var rem = width * 100 / designwidth; //兼容uc开始 rootstyle = "html{font-size:" + rem + 'px !important}'; rootitem = document.getelementbyid('rootsize') || document.createelement("style"); if (!document.getelementbyid('rootsize')) { document.getelementsbytagname("head")[0].appendchild(rootitem); rootitem.id = 'rootsize'; } if (rootitem.stylesheet) { rootitem.stylesheet.disabled || (rootitem.stylesheet.csstext = rootstyle) } else { try { rootitem.innerhtml = rootstyle } catch (f) { rootitem.innertext = rootstyle } } //兼容uc结束 docel.style.fontsize = rem + "px"; }; refreshrem(); win.addeventlistener("resize", function () { cleartimeout(tid); //防止执行两次 tid = settimeout(refreshrem, 300); }, false); win.addeventlistener("pageshow", function (e) { if (e.persisted) { // 浏览器后退的时候重新计算 cleartimeout(tid); tid = settimeout(refreshrem, 300); } }, false); if (doc.readystate === "complete") { doc.body.style.fontsize = "16px"; } else { doc.addeventlistener("domcontentloaded", function (e) { doc.body.style.fontsize = "16px"; }, false); } })(375, 750);