Created
May 2, 2017 10:42
-
-
Save jiangtao/93cde134fa4866080793b764a388bc8d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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