H5 terminal rem adaptive screen

Calculate the size of the design drawing and the window width. The window width is enlarged by 100 times,
that is, 1rem = 100px
is introduced in the head tag, so that the page can fit the screen as soon as it is loaded.

; (function (win) {
    
    
    var tid;
    function refreshRem() {
    
    
        let designSize = 1920; // 设计图尺寸
        let html = document.documentElement;
        let wW = html.clientWidth;// 窗口宽度
        let rem = wW * 100 / designSize;
        document.documentElement.style.fontSize = rem + 'px';
    }

    win.addEventListener('resize', function () {
    
    
        clearTimeout(tid);
        tid = setTimeout(refreshRem, 300);
    }, false);
    win.addEventListener('pageshow', function (e) {
    
    
        if (e.persisted) {
    
    
            clearTimeout(tid);
            tid = setTimeout(refreshRem, 300);
        }
    }, false);

    refreshRem();

})(window);

If you find it useful, please give it a thumbs up, thank you

Follow me and share technical dry goods from time to time~

Scan the QR code to follow the official account
insert image description here

Guess you like

Origin blog.csdn.net/weixin_45295253/article/details/121862710