zoukankan      html  css  js  c++  java
  • sticker-footer 布局

    sticker-footer

    1、嵌套层级不深,可直接继承自 body width:100%; height:100%;

    // html
    <body>
        <div id="sticker">
            <div class="sticker-con">我是内容</div>
        </div>
        <div class="footer">我是脚</div>
    </body>
    
    // css
    html,body{
        100%;
        height:100%;
    }
    #sticker{
        100%;
        min-height:100%;
    }
    .sticker-con{
        padding-bottom:40px;    // 40px 为 footer 本身高度
    }
    .footer{
        margin-top:-40px;  // 40px 为 footer 本身高度
    }
    

    2、嵌套层级很深,无法直接从上级继承 百分比高度的

    • 第一种方法:给需要的 sticker-footer 创建一个 wrapper
        <body>
            <div id="wrapper">
                <div id="sticker">
                    <div class="sticker-con">我是内容</div>
                </div>
                <div class="footer">我是脚</div>
            </div>
        </body>
    
        .wrapper{
            position:fixed;  // 这样 wrapper 就可以直接从 html,body 继承 百分比高度了
            overflow:auto;   // 当高度超过 100% ;时产生滚动条
            100%;
            height:100%;     // 继承自 body
        }
        // wrapper 内部包裹的结构,就如上所示了,css样式也一样

    3. 当无法用百分比获取高度时,也可通过js方式获得

        //css样式同第一种, 只是 sticker 的 min-height 用css获取
    
        <body>
            <div id="sticker">
                <div class="sticker-con">我是内容</div>
            </div>
            <div class="footer">我是脚</div>
        </body>
    
    
        var sticker = document.querySelector('#sticker');
        var h = document.body.clientHeight;
        sticker.style.minHeight = h - 44 + 'px';
    
        //这种方式也可应对一些特殊情况,比如有头部导航栏的情况,可以灵活的处理 min-height:

    4. 强大的 flex 布局 flex-direction:column

    • 将wrapper容器 display:flex; flex-direction:column
    • sticker: flex:1; 占据除footer以外的剩余空间
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no">
        <title>sticker footer</title>
    </head>
    <style>
        html,body{
             100%;
            height: 100%;
            background-color: #ccc;
            margin:0;
            padding: 0;
            
        }
        header{
            height:44px;
             100%;
            text-align: center;
            line-height: 44px;
        }
        #wrapper{
            display: flex;
            flex-direction: column;
             100%;
            /*height: 100%;*/
        }
        #sticker{
            background-color: red;
            flex: 1;
        }
        #sticker .sticker-con{
            padding-bottom: 40px;
        }
        .footer{
            background-color: green;
            height: 40px;
        }
    </style>    
    <body>
    
        <header>我是头部</header>
        <div id="wrapper">
            <div id="sticker">
                <div class="sticker-con">我是内容</div>
            </div>
            <div class="footer">我是脚</div>
        </div>
        
    </body>
    <script>
        var wrapper = document.querySelector('#wrapper');
        var h = document.body.clientHeight;
        wrapper.style.minHeight = h - 44 + 'px';   // 减去头部导航栏高度
    
    </script>
    </html>
  • 相关阅读:
    Python爬虫案例:爬取微信公众号文章
    MySQL计算两坐标距离并排序
    删库了一定要跑路吗?
    在python中列表删除和多重循环退出
    软件设计模式修炼 -- 观察者模式
    C# WPF:快把文件从桌面拖进我的窗体来!
    C#(七)基础篇—基本I/O操作
    (8)ASP.NET Core3.1 Ocelot Consul服务注册与发现
    iNeuOS工业互联操作系统,图表与数据点组合成新组件,进行项目复用
    对于经常接触的分页你确定你真的会吗
  • 原文地址:https://www.cnblogs.com/10manongit/p/12853500.html
Copyright © 2011-2022 走看看