zoukankan      html  css  js  c++  java
  • 手机页面rem布局

    手机页面设计一般的大小是640,但是,手机屏幕大小确实不确定的,这样,怎么才能做出适应所有手机的手机页面呢?
    一般的解决方案有两种,rem布局和百分比布局,更推荐用rem布局来制作手机页面。

    首先,给页面的html定义一个100px的
    html{ font-size:100px;}/*设定基础rem*/

    然后就是这一段js运算了,根据页面的大小来控制基础rem的值;

    new function (){
       var _self = this;
       _self.width = 640;//设置默认最大宽度
       _self.fontSize = 100;//默认字体大小
       _self.widthProportion = function(){var p = (document.body&&document.body.clientWidthdocument.getElementsByTagName("html")[0].offsetWidth)/_self.width;return p>1?1:p<0.5?0.5:p;};
       _self.changePage = function(){
           document.getElementsByTagName("html")[0].setAttribute("style","font-size:"+_self.widthProportion()*_self.fontSize+"px !important");
       }
       _self.changePage();
       window.addEventListener("resize",function(){_self.changePage();},false);
    };

    <!DOCTYPE html>
    <html>
    <head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0">
    <meta charset="utf-8">
    <title>rem基础布局</title>
    <script type="text/javascript">
    new function (){
       var _self = this;
       _self.width = 640;//设置默认最大宽度
       _self.fontSize = 100;//默认字体大小
       _self.widthProportion = function(){var p = (document.body&&document.body.clientWidthdocument.getElementsByTagName("html")[0].offsetWidth)/_self.width;return p>1?1:p<0.5?0.5:p;};
       _self.changePage = function(){
           document.getElementsByTagName("html")[0].setAttribute("style","font-size:"+_self.widthProportion()*_self.fontSize+"px !important");
       }
       _self.changePage();
       window.addEventListener("resize",function(){_self.changePage();},false);
    };
    </script>
    <style type="text/css">
    /*=== base style===*/
    *{margin: 0px; padding: 0px;}
    ul{list-style: none;}
    .wrap{min- 320px; max- 640px;  100%; margin: 0px auto;; background: #2a6ace; font-family:"微软雅黑", "helvetica neue",tahoma,"hiragino sans gb",stheiti,"wenquanyi micro hei",5FAE8F6F96C59ED1,5B8B4F53,sans-serif; font-size: 12px;}/*适用于手机端:字体大小用em,1em=16px;为默认字体大小;最大宽度640*/
    
    .pro{6.2rem; margin: 0px auto; padding-top: 20px; overflow: hidden;}
    .clearfix:after {content:"";height:0;display:block;clear:both;}
    .clearfix {zoom:1;}
    .pro ul{6.4rem;}
    .pro li{ 3rem; height: 3.6rem; float: left; margin: 0 0.2rem 0.2rem 0;}
    .pro li .box{ 3rem; height: 3rem; background: #ccc;}
    .pro li p{font-size: 0.24rem; line-height: 0.6rem; text-align: center;}
    </style>
    </head>
    <body>
    <div class="wrap">
       <div class="pro">
          <ul class="clearfix">
             <li> <div class="box"></div> <p>区块文案</p> </li>
             <li> <div class="box"></div> <p>区块文案</p> </li>
             <li> <div class="box"></div> <p>区块文案</p> </li>
             <li> <div class="box"></div> <p>区块文案</p> </li>
             <li> <div class="box"></div> <p>区块文案</p> </li>
          </ul>
       </div>
    </div>
    </body>
    </html>
    // 监听浏览器,针对不同分辨率计算font-size
    // 然后根据设计稿比如尺寸是640尺寸 rem = 设计稿的字体大小 / 100 ; 16px —> 0.16rem
    
    (function (doc, win) {
        var docEl = doc.documentElement,
            resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize',
            recalc = function () {
                var clientWidth = docEl.clientWidth;
                if (!clientWidth) return;
                if (clientWidth<=320){
                    docEl.style.fontSize = '50px';
                }
                // else if(clientWidth>=640){
                //     docEl.style.fontSize = '100px';
                // }
                else{
                    docEl.style.fontSize = 100 * (clientWidth / 640) + 'px';
                }
            };
        if (!doc.addEventListener) return;
        win.addEventListener(resizeEvt, recalc, false);
        doc.addEventListener('DOMContentLoaded', recalc, false);
    })(document, window);
  • 相关阅读:
    Http方法:Get请求与Post请求的区别
    udev和rules使用规则
    c++中的动态内存分配
    c++中重载运算符
    c++中静态成员函数
    c++中值传递,址传递,引用传递
    c++中实现单例模式singleton class
    [Windows篇] 在windows 10上源码编译gtest 并编写CMakeLists.txt
    [Ubuntu篇] 在ubuntu上源码编译gtest,编写gtest-config.cmake并测试
    使用boost data_time模块来获取毫秒级时间并转换为string字符串
  • 原文地址:https://www.cnblogs.com/wenb/p/5884399.html
Copyright © 2011-2022 走看看