rem.js 651 B

12345678910111213141516171819
  1. (function(doc, win) {
  2. var docEl = doc.documentElement,
  3. resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize',
  4. recalc = function() {
  5. var fontSize = RootNodeFontSize();
  6. if(fontSize == 0) return;
  7. docEl.style.fontSize = fontSize + 'px';
  8. };
  9. if(!doc.addEventListener) return;
  10. win.addEventListener(resizeEvt, recalc, false);
  11. doc.addEventListener('DOMContentLoaded', recalc, false);
  12. })(document, window);
  13. //获取根节点的font-size
  14. function RootNodeFontSize(){
  15. var clientWidth = document.documentElement.clientWidth;
  16. if(!clientWidth) return 0;
  17. if(clientWidth > 750) clientWidth = 750;
  18. return clientWidth / 7.5;
  19. }