index.ts 931 B

1234567891011121314151617181920
  1. // 设置根节点html的字体大小用于移动端适配使用
  2. export const setHtmlFontSize = () => {
  3. console.count("setHtmlFontSize");
  4. (function (doc, win) {
  5. let resizeEvt = "orientationchange" in win ? "orientationchange" : "resize",
  6. recalc = function () {
  7. let htmlDom = doc.getElementsByTagName("html")[0];
  8. requestAnimationFrame(() => {
  9. let clientWidth = doc.documentElement.clientWidth || doc.body.clientWidth;
  10. if (!clientWidth) return;
  11. htmlDom.style.fontSize =
  12. clientWidth >= 578.88 ? "144px" : 154.5 * (clientWidth / 578.88) + "px";
  13. });
  14. };
  15. if (!doc.addEventListener) return;
  16. win.addEventListener(resizeEvt, recalc, false);
  17. doc.addEventListener("DOMContentLoaded", recalc, false);
  18. recalc();
  19. })(document, window);
  20. };