rem布局

<!DOCTYPE html>
<html lang="en" style="font-size: 75px;">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title></title>
        <script>
            // 阻塞 来动态设置 html 的 font-size    // 立即执行函数   
            (function() {
                // console.log('立即执行函数')   
                function calc() {
                    const w = document.documentElement.clientWidth;
                    console.log(w);
                    document.documentElement.style.fontSize = 75 * (w / 750) + 'px';
                    console.log(document.documentElement.style.fontSize);
                }
                calc()
                // 竖屏 横屏  屏幕大小发生变化    
                window.onresize = function() {
                    console.log('屏幕大小发生变化')
                    calc()
                }
            })()
        </script>
        <style>
            * {
                margin: 0;
                padding: 0;
            }
        </style>
    </head>
    <body>
        <div>
            <div style="width: 5rem; height: 2rem;background-color: green;display:inline-block;"></div>
            <div style="width: 5rem; height: 2rem;background-color: blue;display:inline-block;"></div>
        </div>
    </body>
</html>

 

posted @ 2025-02-08 09:41  热心市民~菜先生  阅读(11)  评论(0)    收藏  举报