zoukankan      html  css  js  c++  java
  • vue : rem自适应的应用

    我们知道,rem是一个css单位,指的是HTML根节点的字体大小。

    MDN:css单位

    而我们在用rem布局的时候必然会遇到一个问题:我们需要根据用户的屏幕大小去计算rem。

    以下是代码。

    (在VUE中使用)

    ...
        created() {
            
            // rem 适配
            (function(win) {
                var docEl = win.document.documentElement;
                function refreshRem() {
                    var width = docEl.getBoundingClientRect().width;
                    if (width >= 1920) {
                        // 最大宽度
                        width = 1920;
                    }
                    var rem = width / 19.2;
                    if (rem > 100) {
                        rem = 100;
                    }
                    docEl.style.fontSize = rem + "px";
                    if (width < 1366) {
                        docEl.style.fontSize = 70 + "px";
                    }
                }
                win.addEventListener(
                    "resize",
                    function() {
                        refreshRem();
                    },
                    false
                );
                win.addEventListener(
                    "pageshow",
                    function(e) {
                        if (e.persisted) {
                            refreshRem();
                        }
                    },
                    false
                );
                refreshRem();
            })(window);
            
        },
    ...

    ===

    (在原生项目中使用)

    rem.js

    (function(win) {
        var docEl = win.document.documentElement;
        function refreshRem() {
            var width = docEl.getBoundingClientRect().width;
            if (width >= 1920) {
                // 最大宽度
                width = 1920;
            }
            var rem = width / 19.2;
            if (rem > 100) {
                rem = 100;
            }
            docEl.style.fontSize = rem + "px";
            if (width < 1366) {
                docEl.style.fontSize = 70 + "px";
            }
        }
        win.addEventListener(
            "resize",
            function() {
                refreshRem();
            },
            false
        );
        win.addEventListener(
            "pageshow",
            function(e) {
                if (e.persisted) {
                    refreshRem();
                }
            },
            false
        );
        refreshRem();
    })(window);

    index.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <!-- 引入rem -->
        <script src="./js/rem.js" type="text/javascript"></script>
        <title>...</title>
    </head>
    ...

    以上。

  • 相关阅读:
    在win2003中发布部署vs2010b2写的mvc2网站
    安装blender2.5Alpha0
    Win7下虚拟机个人使用小结:Virtual PC,VMware和VirtualBox
    ASP.NET AJAX Control Toolkit Beta 0911 发布[再增两控件]
    Camtasia 6录屏时鼠标闪烁问题解决
    为XNA制做安装程序(四)WIX Toolset 3.0 for Visual Studio 2008
    Oracle EM 12c
    无题
    从徐汇到虹口
    近况
  • 原文地址:https://www.cnblogs.com/foxcharon/p/10410433.html
Copyright © 2011-2022 走看看