auto-size.js 818 B

12345678910111213141516171819202122232425262728
  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. console.log(winWidth);
  10. doc.documentElement.style.fontSize = (winWidth / 1080) * 100 + 'px';
  11. }
  12. var evt = 'onorientationchange' in win ? 'orientationchange' : 'resize';
  13. var timer = null;
  14. win.addEventListener(evt, function() {
  15. clearTimeout(timer);
  16. timer = setTimeout(setFontSize, 300);
  17. }, false);
  18. win.addEventListener("pageshow", function(e) {
  19. if (e.persisted) {
  20. clearTimeout(timer);
  21. timer = setTimeout(setFontSize, 300);
  22. }
  23. }, false);
  24. //初始化
  25. setFontSize();
  26. }(window, document));