index.html 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <!DOCTYPE html>
  2. <html lang="">
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  6. <meta name="viewport" content="width=device-width,initial-scale=1.0" />
  7. <link rel="icon" href="<%= BASE_URL %>favicon.ico" />
  8. <title><%= htmlWebpackPlugin.options.title %></title>
  9. <!-- 引入样式 -->
  10. <link
  11. rel="stylesheet"
  12. href="https://unpkg.com/element-ui/lib/theme-chalk/index.css"
  13. />
  14. <!-- 引入组件库 -->
  15. <script src="https://unpkg.com/element-ui/lib/index.js"></script>
  16. <script
  17. src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"
  18. prefetch
  19. ></script>
  20. </head>
  21. <body>
  22. <noscript>
  23. <strong
  24. >We're sorry but <%= htmlWebpackPlugin.options.title %> doesn't work
  25. properly without JavaScript enabled. Please enable it to
  26. continue.</strong
  27. >
  28. </noscript>
  29. <div id="app"></div>
  30. <!-- built files will be auto injected -->
  31. </body>
  32. <script>
  33. (function (designWidth, maxWidth) {
  34. var doc = document,
  35. win = window,
  36. docEl = doc.documentElement,
  37. remStyle = document.createElement("style"),
  38. tid;
  39. function refreshRem() {
  40. var width = docEl.getBoundingClientRect().width; //屏幕宽度
  41. maxWidth = maxWidth || 540; //设置最大宽度
  42. width < 800 && (width = 800); //设置最小宽度
  43. width > maxWidth && (width = maxWidth);
  44. var rem = (width * 100) / designWidth; //屏幕宽度 / 设计稿宽度 * 100,若为电脑运行,此时rem=100
  45. remStyle.innerHTML = "html{font-size:" + rem + "px;}"; //此时重新定义html根元素大小为1rem,即100px
  46. }
  47. if (docEl.firstElementChild) {
  48. docEl.firstElementChild.appendChild(remStyle);
  49. } else {
  50. var wrap = doc.createElement("div");
  51. wrap.appendChild(remStyle);
  52. doc.write(wrap.innerHTML);
  53. wrap = null;
  54. }
  55. //要等 wiewport 设置好后才能执行 refreshRem,不然 refreshRem 会执行2次;
  56. refreshRem();
  57. win.addEventListener(
  58. "resize",
  59. function () {
  60. clearTimeout(tid); //防止执行两次
  61. tid = setTimeout(refreshRem, 300);
  62. },
  63. false
  64. );
  65. win.addEventListener(
  66. "pageshow",
  67. function (e) {
  68. if (e.persisted) {
  69. // 浏览器后退的时候重新计算
  70. clearTimeout(tid);
  71. tid = setTimeout(refreshRem, 300);
  72. }
  73. },
  74. false
  75. );
  76. if (doc.readyState === "complete") {
  77. doc.body.style.fontSize = "16px";
  78. } else {
  79. doc.addEventListener(
  80. "DOMContentLoaded",
  81. function (e) {
  82. doc.body.style.fontSize = "16px";
  83. },
  84. false
  85. );
  86. }
  87. })(1920); //此处传入设计稿宽度及最大宽度
  88. </script>
  89. </html>