flexible.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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, e = a.document,
  9. f = e.documentElement,
  10. g = e.querySelector('meta[name="viewport"]'),
  11. h = e.querySelector('meta[name="flexible"]'),
  12. i = 0,
  13. j = 0,
  14. k = b.flexible || (b.flexible = {});
  15. if (g) {
  16. console.warn("将根据已有的meta标签来设置缩放比例");
  17. var l = g.getAttribute("content").match(/initial\-scale=([\d\.]+)/);
  18. l && (j = parseFloat(l[1]), i = parseInt(1 / j))
  19. } else if (h) {
  20. var m = h.getAttribute("content");
  21. if (m) {
  22. var n = m.match(/initial\-dpr=([\d\.]+)/),
  23. o = m.match(/maximum\-dpr=([\d\.]+)/);
  24. n && (i = parseFloat(n[1]), j = parseFloat((1 / i).toFixed(2))), o && (i = parseFloat(o[1]), j = parseFloat((1 / i).toFixed(2)))
  25. }
  26. }
  27. if (!i && !j) {
  28. var p = a.navigator.userAgent,
  29. q = (!!p.match(/android/gi), !!p.match(/iphone/gi)),
  30. r = q && !!p.match(/OS 9_3/),
  31. s = a.devicePixelRatio;
  32. i = q && !r ? s >= 3 && (!i || i >= 3) ? 3 : s >= 2 && (!i || i >= 2) ? 2 : 1 : 1, j = 1 / i
  33. }
  34. if (f.setAttribute("data-dpr", i), !g)
  35. if (g = e.createElement("meta"), g.setAttribute("name", "viewport"), g.setAttribute("content", "initial-scale=" + j + ", maximum-scale=" + j + ", minimum-scale=" + j + ", user-scalable=no"), f.firstElementChild) f.firstElementChild.appendChild(g);
  36. else {
  37. var t = e.createElement("div");
  38. t.appendChild(g), e.write(t.innerHTML)
  39. } a.addEventListener("resize", function () {
  40. clearTimeout(d), d = setTimeout(c, 300)
  41. }, !1), a.addEventListener("pageshow", function (a) {
  42. a.persisted && (clearTimeout(d), d = setTimeout(c, 300))
  43. }, !1), "complete" === e.readyState ? e.body.style.fontSize = 12 * i + "px" : e.addEventListener("DOMContentLoaded", function () {
  44. e.body.style.fontSize = 12 * i + "px"
  45. }, !1), c(), k.dpr = a.dpr = i, k.refreshRem = c, k.rem2px = function (a) {
  46. var b = parseFloat(a) * this.rem;
  47. return "string" == typeof a && a.match(/rem$/) && (b += "px"), b
  48. }, k.px2rem = function (a) {
  49. var b = parseFloat(a) / this.rem;
  50. return "string" == typeof a && a.match(/px$/) && (b += "rem"), b
  51. }
  52. }(window, window.lib || (window.lib = {}));