flexible.js 3.0 KB

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