You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

36 lines
1.2 KiB

2 years ago
  1. <html lang="en">
  2. <head>
  3. <meta charset="UTF-8">
  4. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. <style>
  8. html {
  9. font-size: 10px;
  10. }
  11. div {
  12. /* rem 等于根元素 html 设置为准,这里的 1rem 等于 16px */
  13. font-size: 1rem;
  14. }
  15. </style>
  16. </head>
  17. <body>
  18. <div>
  19. <span>123</span>
  20. </div>
  21. </body>
  22. <script>
  23. var c = () => {
  24. // 获取当前设备宽度
  25. let w = document.documentElement.clientWidth
  26. // 设置字号,默认为字号为20, 以 320 的宽度作为设备依据(可选),如果计算出当前设备的字号大于40,就以40字号显示
  27. let n = (20*(w / 320 ) > 40 ? 40 + "px" : 20*(w / 320 ) + "px")
  28. // 当计算好后,赋值给 fontSize
  29. document.documentElement.style.fontSize = n
  30. }
  31. // 设置为初始化的效果
  32. window.addEventListener("load", c) // load 是重新加载计算
  33. // 当屏幕尺寸改变后的效果
  34. window.addEventListener("resize", c) // resize 属性规定是否可由用户调整元素的尺寸大小
  35. </script>
  36. </html>