flexible.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. !(function (a, b) {
  2. function c() {
  3. var b = f.getBoundingClientRect().width;
  4. b / i > 420 && (b = 420 * i);
  5. var c = b / 10;
  6. (f.style.fontSize = c + 'px'), (k.rem = a.rem = c);
  7. }
  8. var d,
  9. e = a.document,
  10. f = e.documentElement,
  11. g = e.querySelector('meta[name="viewport"]'),
  12. h = e.querySelector('meta[name="flexible"]'),
  13. i = 0,
  14. j = 0,
  15. k = b.flexible || (b.flexible = {});
  16. if (g) {
  17. console.warn('将根据已有的meta标签来设置缩放比例');
  18. var l = g.getAttribute('content').match(/initial\-scale=([\d\.]+)/);
  19. l && ((j = parseFloat(l[1])), (i = parseInt(1 / j)));
  20. } else if (h) {
  21. var m = h.getAttribute('content');
  22. if (m) {
  23. var n = m.match(/initial\-dpr=([\d\.]+)/),
  24. o = m.match(/maximum\-dpr=([\d\.]+)/);
  25. n && ((i = parseFloat(n[1])), (j = parseFloat((1 / i).toFixed(2)))),
  26. o && ((i = parseFloat(o[1])), (j = parseFloat((1 / i).toFixed(2))));
  27. }
  28. }
  29. if (!i && !j) {
  30. var p = a.navigator.userAgent,
  31. q = (!!p.match(/android/gi), !!p.match(/iphone/gi)),
  32. r = q && !!p.match(/OS 9_3/),
  33. s = a.devicePixelRatio;
  34. (i =
  35. q && !r
  36. ? s >= 3 && (!i || i >= 3)
  37. ? 3
  38. : s >= 2 && (!i || i >= 2)
  39. ? 2
  40. : 1
  41. : 1),
  42. (j = 1 / i);
  43. }
  44. if ((f.setAttribute('data-dpr', i), !g))
  45. if (
  46. ((g = e.createElement('meta')),
  47. g.setAttribute('name', 'viewport'),
  48. g.setAttribute(
  49. 'content',
  50. 'initial-scale=' +
  51. j +
  52. ', maximum-scale=' +
  53. j +
  54. ', minimum-scale=' +
  55. j +
  56. ', user-scalable=no'
  57. ),
  58. f.firstElementChild)
  59. )
  60. f.firstElementChild.appendChild(g);
  61. else {
  62. var t = e.createElement('div');
  63. t.appendChild(g), e.write(t.innerHTML);
  64. }
  65. a.addEventListener(
  66. 'resize',
  67. function () {
  68. clearTimeout(d), (d = setTimeout(c, 300));
  69. },
  70. !1
  71. ),
  72. a.addEventListener(
  73. 'pageshow',
  74. function (a) {
  75. a.persisted && (clearTimeout(d), (d = setTimeout(c, 300)));
  76. },
  77. !1
  78. ),
  79. 'complete' === e.readyState
  80. ? (e.body.style.fontSize = 12 * i + 'px')
  81. : e.addEventListener(
  82. 'DOMContentLoaded',
  83. function () {
  84. e.body.style.fontSize = 12 * i + 'px';
  85. },
  86. !1
  87. ),
  88. c(),
  89. (k.dpr = a.dpr = i),
  90. (k.refreshRem = c),
  91. (k.rem2px = function (a) {
  92. var b = parseFloat(a) * this.rem;
  93. return 'string' == typeof a && a.match(/rem$/) && (b += 'px'), b;
  94. }),
  95. (k.px2rem = function (a) {
  96. var b = parseFloat(a) / this.rem;
  97. return 'string' == typeof a && a.match(/px$/) && (b += 'rem'), b;
  98. });
  99. })(window, window.lib || (window.lib = {}));