您的位置:博客首页 >> 程序源码 >> 自动缩放字体size js

自动缩放字体size js

日志编号:268 发表时间: 2020-02-23 00:59:45 关注次数:295
[code]       !new
                    function() {
                        var a = this;
                        a.width = 750,
                                a.fontSize = 10,
                                a.widthProportion = function() {
                                    var b = (document.body && document.body.clientWidth || document.getElementsByTagName("html")[0].offsetWidth) / a.width;
                                    return b > 1 ? 1 : b
                                },
                                a.changePage = function() {
                                    document.getElementsByTagName("html")[0].setAttribute("style", "font-size:" + a.widthProportion() * a.fontSize + "px !important")
                                },
                                a.changePage(),
                                window.addEventListener("resize",
                                        function() {
                                            a.changePage()
                                        },
                                        !1)
                    };
  [/code]  

 <script>
        window.onload = function(){
            getRem(720,100)
        };
        window.onresize = function(){
            getRem(720,100)
        };
        function getRem(pwidth,prem){
            var html = document.getElementsByTagName("html")[0];
            var oWidth = document.body.clientWidth || document.documentElement.clientWidth;
            html.style.fontSize = oWidth/pwidth*prem + "px";
        }
        /*
        //小米官网的写法
        !function(n){
            var  e=n.document,
                 t=e.documentElement,
                 i=720,
                 d=i/100,
                 o="orientationchange"in n?"orientationchange":"resize",
                 a=function(){
                     var n=t.clientWidth||320;n>720&&(n=720);
                     t.style.fontSize=n/d+"px"
                 };
                 e.addEventListener&&(n.addEventListener(o,a,!1),e.addEventListener("DOMContentLoaded",a,!1))
        }(window);*/
    </script>
本站不提供讨论功能。
本站所有非新闻类文章均为原创,且禁止转载。
本站为了获得更多流量赚取广告费,难免会有以次充好的文章,望见谅,勿鄙视。