Skip to content

Instantly share code, notes, and snippets.

@jiangtao
Created May 2, 2017 10:42
Show Gist options
  • Save jiangtao/93cde134fa4866080793b764a388bc8d to your computer and use it in GitHub Desktop.
Save jiangtao/93cde134fa4866080793b764a388bc8d to your computer and use it in GitHub Desktop.
(function (win) {
var ratio, scaleValue, renderTime,
document = window.document,
docElem = document.documentElement,
vpm = document.querySelector('meta[name="viewport"]');
if (vpm) {
var tempArray = vpm.getAttribute("content").match(/initial\-scale=(["']?)([\d\.]+)\1?/);
if (tempArray) {
scaleValue = parseFloat(tempArray[2]);
ratio = parseInt(1 / scaleValue);
}
} else {
vpm = document.createElement("meta");
vpm.setAttribute("name", "viewport");
vpm.setAttribute("content", "width=device-width, initial-scale=1, user-scalable=no, minimal-ui");
docElem.firstElementChild.appendChild(vpm);
}
win.addEventListener("resize", function () {
clearTimeout(renderTime);
renderTime = setTimeout(initPage, 300);
}, false);
win.addEventListener("pageshow", function (e) {
e.persisted && (clearTimeout(renderTime), renderTime = setTimeout(initPage, 300));
}, false);
"complete" === document.readyState ? document.body.style.fontSize = 12 * ratio + "px" : document.addEventListener("DOMContentLoaded", function () {
document.body.style.fontSize = 12 * ratio + "px";
}, false);
initPage();
function initPage() {
var htmlWidth = docElem.getBoundingClientRect().width;
htmlWidth / ratio > 540 && (htmlWidth = 540 * ratio);
win.rem = htmlWidth / 7.5;
docElem.style.fontSize = win.rem + "px";
}
})(window);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment