Skip to content

Instantly share code, notes, and snippets.

@xiangst0816
Last active January 14, 2016 07:55
Show Gist options
  • Select an option

  • Save xiangst0816/697454d1ad9402fba208 to your computer and use it in GitHub Desktop.

Select an option

Save xiangst0816/697454d1ad9402fba208 to your computer and use it in GitHub Desktop.
根据窗口宽度大小,确定html的fontsize大小,之后用rem作为尺寸单元
<script>
(function(doc, win) {
var docEl = doc.documentElement,
resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize',
recalc = function() {
var clientWidth = docEl.clientWidth;
if (!clientWidth)
return;
//由屏幕宽度得到basefont大小,并将其写入html标签中,文档总的尺寸由rem确定
docEl.style.fontSize = 100 * (clientWidth / 640) + 'px';
doc.body.style.display = "block";
doc.getElementsByTagName('html')[0].removeAttribute('class');
};
if (!doc.addEventListener)
return;
win.addEventListener(resizeEvt, recalc, false);
doc.addEventListener('DOMContentLoaded', recalc, false);
})(document, window);
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment