auto-size.js 786 B

12345678910111213141516171819202122232425
  1. /**
  2. * Created by lovo_bdk on 15-12-17.
  3. */
  4. !(function(win, doc) {
  5. function setFontSize() {
  6. // 获取window 宽度
  7. // zepto实现 $(window).width()就是这么干的
  8. var winWidth = window.innerWidth;
  9. doc.documentElement.style.fontSize = (winWidth / 1080) * 100 + 'px';
  10. }
  11. var evt = 'onorientationchange' in win ? 'orientationchange' : 'resize';
  12. var timer = null;
  13. win.addEventListener(evt, function() {
  14. clearTimeout(timer);
  15. timer = setTimeout(setFontSize, 300);
  16. }, false);
  17. win.addEventListener("pageshow", function(e) {
  18. if (e.persisted) {
  19. clearTimeout(timer);
  20. timer = setTimeout(setFontSize, 300);
  21. }
  22. }, false);
  23. //初始化
  24. setFontSize();
  25. }(window, document));