flexible.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. (function(win, lib) {
  2. var doc = win.document;
  3. var docEl = doc.documentElement;
  4. var metaEl = doc.querySelector('meta[name="viewport"]');
  5. var flexibleEl = doc.querySelector('meta[name="flexible"]');
  6. var dpr = 0;
  7. var scale = 0;
  8. var tid;
  9. var flexible = lib.flexible || (lib.flexible = {});
  10. if (metaEl) {
  11. console.warn('将根据已有的meta标签来设置缩放比例');
  12. var match = metaEl.getAttribute('content').match(/initial\-scale=([\d\.]+)/);
  13. if (match) {
  14. scale = parseFloat(match[1]);
  15. dpr = parseInt(1 / scale);
  16. }
  17. } else if (flexibleEl) {
  18. var content = flexibleEl.getAttribute('content');
  19. if (content) {
  20. var initialDpr = content.match(/initial\-dpr=([\d\.]+)/);
  21. var maximumDpr = content.match(/maximum\-dpr=([\d\.]+)/);
  22. if (initialDpr) {
  23. dpr = parseFloat(initialDpr[1]);
  24. scale = parseFloat((1 / dpr).toFixed(2));
  25. }
  26. if (maximumDpr) {
  27. dpr = parseFloat(maximumDpr[1]);
  28. scale = parseFloat((1 / dpr).toFixed(2));
  29. }
  30. }
  31. }
  32. if (!dpr && !scale) {
  33. var isAndroid = win.navigator.appVersion.match(/android/gi);
  34. var isIPhone = win.navigator.appVersion.match(/iphone/gi);
  35. var devicePixelRatio = win.devicePixelRatio;
  36. if (isIPhone) {
  37. // iOS下,对于2和3的屏,用2倍的方案,其余的用1倍方案
  38. if (devicePixelRatio >= 3 && (!dpr || dpr >= 3)) {
  39. dpr = 3;
  40. } else if (devicePixelRatio >= 2 && (!dpr || dpr >= 2)){
  41. dpr = 2;
  42. } else {
  43. dpr = 1;
  44. }
  45. } else {
  46. // 其他设备下,仍旧使用1倍的方案
  47. dpr = 1;
  48. }
  49. scale = 1 / dpr;
  50. }
  51. docEl.setAttribute('data-dpr', dpr);
  52. if (!metaEl) {
  53. metaEl = doc.createElement('meta');
  54. metaEl.setAttribute('name', 'viewport');
  55. metaEl.setAttribute('content', 'initial-scale=' + scale + ', maximum-scale=' + scale + ', minimum-scale=' + scale + ', user-scalable=no,viewport-fit=cover');
  56. if (docEl.firstElementChild) {
  57. docEl.firstElementChild.appendChild(metaEl);
  58. } else {
  59. var wrap = doc.createElement('div');
  60. wrap.appendChild(metaEl);
  61. doc.write(wrap.innerHTML);
  62. }
  63. }
  64. function refreshRem(){
  65. var width = docEl.getBoundingClientRect().width;
  66. if (width / dpr > 540) {
  67. width = 540 * dpr;
  68. }
  69. var rem = width / 10;
  70. docEl.style.fontSize = rem + 'px';
  71. flexible.rem = win.rem = rem;
  72. }
  73. win.addEventListener('resize', function() {
  74. clearTimeout(tid);
  75. tid = setTimeout(refreshRem, 300);
  76. }, false);
  77. win.addEventListener('pageshow', function(e) {
  78. if (e.persisted) {
  79. clearTimeout(tid);
  80. tid = setTimeout(refreshRem, 300);
  81. }
  82. }, false);
  83. if (doc.readyState === 'complete') {
  84. doc.body.style.fontSize = 12 * dpr + 'px';
  85. } else {
  86. doc.addEventListener('DOMContentLoaded', function(e) {
  87. doc.body.style.fontSize = 12 * dpr + 'px';
  88. }, false);
  89. }
  90. refreshRem();
  91. flexible.dpr = win.dpr = dpr;
  92. flexible.refreshRem = refreshRem;
  93. flexible.rem2px = function(d) {
  94. var val = parseFloat(d) * this.rem;
  95. if (typeof d === 'string' && d.match(/rem$/)) {
  96. val += 'px';
  97. }
  98. return val;
  99. }
  100. flexible.px2rem = function(d) {
  101. var val = parseFloat(d) / this.rem;
  102. if (typeof d === 'string' && d.match(/px$/)) {
  103. val += 'rem';
  104. }
  105. return val;
  106. }
  107. })(window, window['lib'] || (window['lib'] = {}));