
本文分享如何使用JavaScript监听窗口尺寸变化调整根元素的字体大小,以便于达到自适应效果,当屏幕大于450px时 10rem等于450px 当屏幕小于等于450px的时候 10rem等于100%
主要js
<script> function autoPx() { function e() { var e = window.innerWidth / 10; document.querySelector("html").style.cssText = "font-size:" + (45 < e ? 45 : e) + "px" } e(), window.addEventListener("resize", e) } autoPx() </script>