flexible.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. ! function (e, t) {
  2. function i() {
  3. var t = n.getBoundingClientRect().width / 10;
  4. n.style.fontSize = t + "px", p.rem = e.rem = t
  5. }
  6. var a, r = e.document,
  7. n = r.documentElement,
  8. o = r.querySelector('meta[name="viewport"]'),
  9. l = r.querySelector('meta[name="flexible"]'),
  10. m = r.querySelector('meta[name="flexible-in-x5"]'),
  11. s = !0,
  12. d = 0,
  13. c = 0,
  14. p = t.flexible || (t.flexible = {});
  15. if (o) {
  16. console.warn("将根据已有的meta标签来设置缩放比例");
  17. var u = o.getAttribute("content").match(/initial\-scale=([\d\.]+)/);
  18. u && (c = parseFloat(u[1]), d = parseInt(1 / c))
  19. } else if (l) {
  20. var f = l.getAttribute("content");
  21. if (f) {
  22. var h = f.match(/initial\-dpr=([\d\.]+)/),
  23. v = f.match(/maximum\-dpr=([\d\.]+)/);
  24. h && (d = parseFloat(h[1]), c = parseFloat((1 / d).toFixed(2))), v && (d = parseFloat(v[1]), c = parseFloat((1 / d).toFixed(2)))
  25. }
  26. }
  27. if (m && (s = "false" !== m.getAttribute("content")), !d && !c) {
  28. var x = (e.navigator.appVersion.match(/android/gi), e.chrome),
  29. g = e.navigator.appVersion.match(/iphone/gi),
  30. b = e.devicePixelRatio,
  31. w = /TBS\/\d+/.test(e.navigator.userAgent),
  32. y = !1;
  33. try {
  34. y = "true" === localStorage.getItem("IN_FLEXIBLE_WHITE_LIST")
  35. } catch (e) {
  36. y = !1
  37. }
  38. c = 1 / (d = g || x || w && s && y ? b >= 3 && (!d || d >= 3) ? 3 : b >= 2 && (!d || d >= 2) ? 2 : 1 : 1)
  39. }
  40. if (n.setAttribute("data-dpr", d), !o)
  41. if ((o = r.createElement("meta")).setAttribute("name", "viewport"), o.setAttribute("content", "initial-scale=" + c + ", maximum-scale=" + c + ", minimum-scale=" + c + ", user-scalable=no"), n.firstElementChild) n.firstElementChild.appendChild(o);
  42. else {
  43. var E = r.createElement("div");
  44. E.appendChild(o), r.write(E.innerHTML)
  45. }
  46. e.addEventListener("resize", function () {
  47. clearTimeout(a), a = setTimeout(i, 300)
  48. }, !1), e.addEventListener("pageshow", function (e) {
  49. e.persisted && (clearTimeout(a), a = setTimeout(i, 300))
  50. }, !1), "complete" === r.readyState ? r.body.style.fontSize = 12 * d + "px" : r.addEventListener("DOMContentLoaded", function (e) {
  51. r.body.style.fontSize = 12 * d + "px"
  52. }, !1), i(), p.dpr = e.dpr = d, p.refreshRem = i, p.rem2px = function (e) {
  53. var t = parseFloat(e) * this.rem;
  54. return "string" == typeof e && e.match(/rem$/) && (t += "px"), t
  55. }, p.px2rem = function (e) {
  56. var t = parseFloat(e) / this.rem;
  57. return "string" == typeof e && e.match(/px$/) && (t += "rem"), t
  58. }
  59. }(window, window.lib || (window.lib = {}))