适配屏幕文字大小

直接上代码:
<script>
    fnResize();
    window.onresize = function () {
        fnResize();
        window.addEventListener("resize", fnResize);
    };
    function fnResize() {
        let devWidth = document.documentElement.clientWidth || window.innerWidth;//浏览器视口宽度
        let equipment = (navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i))
        console.log(devWidth, equipment)
        if (equipment) {
            if (devWidth >= 750) {
                devWidth = 750;
            }
            if (devWidth <= 320) {
                devWidth = 320;
            }
            document.documentElement.style.fontSize = parseInt(devWidth / 23) + "px";
        } else {
            document.documentElement.style.fontSize = 16 + "px";
        }
    }
</script>
posted on 2022-03-30 17:15  mydiary  阅读(38)  评论(0)    收藏  举报