移动端屏幕适配——简单适配方案

时间:2022-04-22 18:48:33

html字体大小随屏幕大小的改变而改变,封装函数如下:

 

(function(){
    ‘use strict‘;
    
    setRemUnit();
    window.addEventListener(‘resize‘,setRemUnit);
    function setRemUnit(){
        var docEl = document.documentElement;
        var ratio = 18.75;
        var viewWidth = docEl.getBoundingClientRect().width || window.innerWidth;
        docEl.style.fontSize = viewWidth / ratio   ‘px‘;
    }
})();