flexible.js 2.5 KB

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