Skip to content

Instantly share code, notes, and snippets.

@creeperyang
Created November 28, 2016 10:42
Show Gist options
  • Save creeperyang/204a87540c3fa59fa03b426e38331ac4 to your computer and use it in GitHub Desktop.
Save creeperyang/204a87540c3fa59fa03b426e38331ac4 to your computer and use it in GitHub Desktop.
rem helper
(function (doc, win) {
var docEl = doc.documentElement,
isIPhone = window.navigator.appVersion.match(/iphone/gi),
platform = navigator.platform,
justMobile = !/win32/i.test(platform),
fontSize, scale;
recalc();
function recalc() {
var clientWidth = docEl.clientWidth;
var dpr = window.devicePixelRatio;
// iOS下,对于2和3的屏,用2倍的方案,其余的用1倍方案。其他设备下,仍旧使用1倍的方案
if (!isIPhone || !justMobile)) {
dpr = 1;
clientWidth = clientWidth ? clientWidth : (window.screen.width || 360);
}
scale = 1 / (dpr > 1 ? 2 : dpr);
fontSize = 20 * (clientWidth / 320) / scale;
fontSize = (fontSize > 54) ? 54: fontSize;
// 如果fontSize是小数,取小于它的偶数
if (~~fontSize !== fontSize) {
fontSize = ~~(fontSize / 2) * 2
}
docEl.style.fontSize = fontSize + 'px';
docEl.setAttribute('data-dpr', dpr);
//设置viewport
var viewport = document.querySelector('meta[name="viewport"]');
var viewport_content = 'initial-scale=' + scale + ', maximum-scale=' + scale + ', minimum-scale=' + scale + ', user-scalable=no';
viewport && viewport.setAttribute('content', viewport_content);
};
})(document, window);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment