25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

147 lines
5.1 KiB

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Document</title>
  6. <meta name="viewport" content="initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no">
  7. <style>
  8. html, body, div, span, applet, object, iframe, h1, h2, h3, h4, h5, h6, em, p, font, img, small, strong, center, dl, dt, dd, ol, ul, li, form, label, table, caption, tbody, tr, th, td {margin:0;padding:0;border:0;outline:0;font-size:12px;vertical-align:baseline}
  9. ol, ul {list-style:none} :focus {outline:none; }
  10. table {border-collapse:collapse;border-spacing: 0}
  11. caption, th, td { text-align: left; font-weight: normal}
  12. strong {font-weight:600}
  13. a{font:12px Arial;text-decoration:none;color:#404040;cursor:pointer;}
  14. a:hover{text-decoration:underline;color:#FF3300}
  15. h1{font-size:16px;font-weight:600;color:#666} .left{float:left} .right{float:right} .clear{clear:both}
  16. </style>
  17. </head>
  18. <script>
  19. ;(function(win, lib) {
  20. var doc = win.document;
  21. var docEl = doc.documentElement;
  22. var metaEl = doc.querySelector('meta[name="viewport"]');
  23. var flexibleEl = doc.querySelector('meta[name="flexible"]');
  24. var dpr = 0;
  25. var scale = 0;
  26. var tid;
  27. var flexible = lib.flexible || (lib.flexible = {});
  28. if (metaEl) {
  29. console.warn('将根据已有的meta标签来设置缩放比例');
  30. var match = metaEl.getAttribute('content').match(/initial\-scale=([\d\.]+)/);
  31. if (match) {
  32. scale = parseFloat(match[1]);
  33. dpr = parseInt(1 / scale);
  34. }
  35. } else if (flexibleEl) {
  36. var content = flexibleEl.getAttribute('content');
  37. if (content) {
  38. var initialDpr = content.match(/initial\-dpr=([\d\.]+)/);
  39. var maximumDpr = content.match(/maximum\-dpr=([\d\.]+)/);
  40. if (initialDpr) {
  41. dpr = parseFloat(initialDpr[1]);
  42. scale = parseFloat((1 / dpr).toFixed(2));
  43. }
  44. if (maximumDpr) {
  45. dpr = parseFloat(maximumDpr[1]);
  46. scale = parseFloat((1 / dpr).toFixed(2));
  47. }
  48. }
  49. }
  50. if (!dpr && !scale) {
  51. var isAndroid = win.navigator.appVersion.match(/android/gi);
  52. var isIPhone = win.navigator.appVersion.match(/iphone/gi);
  53. var devicePixelRatio = win.devicePixelRatio;
  54. if (isIPhone) {
  55. // iOS下,对于2和3的屏,用2倍的方案,其余的用1倍方案
  56. if (devicePixelRatio >= 3 && (!dpr || dpr >= 3)) {
  57. dpr = 3;
  58. } else if (devicePixelRatio >= 2 && (!dpr || dpr >= 2)){
  59. dpr = 2;
  60. } else {
  61. dpr = 1;
  62. }
  63. } else {
  64. // 其他设备下,仍旧使用1倍的方案
  65. dpr = 1;
  66. }
  67. scale = 1 / dpr;
  68. }
  69. docEl.setAttribute('data-dpr', dpr);
  70. if (!metaEl) {
  71. metaEl = doc.createElement('meta');
  72. metaEl.setAttribute('name', 'viewport');
  73. metaEl.setAttribute('content', 'initial-scale=' + scale + ', maximum-scale=' + scale + ', minimum-scale=' + scale + ', user-scalable=no');
  74. if (docEl.firstElementChild) {
  75. docEl.firstElementChild.appendChild(metaEl);
  76. } else {
  77. var wrap = doc.createElement('div');
  78. wrap.appendChild(metaEl);
  79. doc.write(wrap.innerHTML);
  80. }
  81. }
  82. function refreshRem(){
  83. var width = docEl.getBoundingClientRect().width;
  84. if (width / dpr > 540) {
  85. width = 540 * dpr;
  86. }
  87. var rem = width / 10;
  88. docEl.style.fontSize = rem + 'px';
  89. flexible.rem = win.rem = rem;
  90. }
  91. win.addEventListener('resize', function() {
  92. clearTimeout(tid);
  93. tid = setTimeout(refreshRem, 300);
  94. }, false);
  95. win.addEventListener('pageshow', function(e) {
  96. if (e.persisted) {
  97. clearTimeout(tid);
  98. tid = setTimeout(refreshRem, 300);
  99. }
  100. }, false);
  101. if (doc.readyState === 'complete') {
  102. doc.body.style.fontSize = 12 * dpr + 'px';
  103. } else {
  104. doc.addEventListener('DOMContentLoaded', function(e) {
  105. doc.body.style.fontSize = 12 * dpr + 'px';
  106. }, false);
  107. }
  108. refreshRem();
  109. flexible.dpr = win.dpr = dpr;
  110. flexible.refreshRem = refreshRem;
  111. flexible.rem2px = function(d) {
  112. var val = parseFloat(d) * this.rem;
  113. if (typeof d === 'string' && d.match(/rem$/)) {
  114. val += 'px';
  115. }
  116. return val;
  117. }
  118. flexible.px2rem = function(d) {
  119. var val = parseFloat(d) / this.rem;
  120. if (typeof d === 'string' && d.match(/px$/)) {
  121. val += 'rem';
  122. }
  123. return val;
  124. }
  125. })(window, window['lib'] || (window['lib'] = {}));
  126. </script>
  127. <body>
  128. <span style="font-size:10rem; background:#e60012;">大</span>
  129. <div style="width: 10rem; background:#e60112;">大</div>
  130. <div style="width: 100%; background:#e60212;">大</div>
  131. <div style="font-size:10rem;width: 10rem; background:#e60312;">大</div>
  132. </body>
  133. </html>