flexible.js 2.7 KB

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