index.html 2.6 KB

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