rem.js 814 B

12345678910111213141516171819202122232425262728293031
  1. // 设置 rem 函数
  2. export function setRem() {
  3. const doc = document
  4. const h = Math.max(doc.documentElement.clientHeight, window.innerHeight || 0)
  5. const w = Math.max(doc.documentElement.clientWidth, window.innerWidth || 0)
  6. let width = w
  7. width = width > 750 ? 750 : width
  8. const fz = ~~((width * 100000) / 100) / 10000
  9. doc.getElementsByTagName('html')[0].style.cssText = 'font-size: ' + fz + 'px'
  10. const realfz =
  11. ~~(
  12. +window
  13. .getComputedStyle(doc.getElementsByTagName('html')[0])
  14. .fontSize.replace('px', '') * 10000
  15. ) / 10000
  16. if (fz !== realfz) {
  17. doc.getElementsByTagName('html')[0].style.cssText =
  18. 'font-size: ' + fz * (fz / realfz) + 'px'
  19. }
  20. }
  21. // 初始化
  22. setRem()
  23. // 改变窗口大小时重新设置 rem
  24. window.onresize = function() {
  25. setRem()
  26. }