flexible.js 2.8 KB

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