/*********************************************************************
 *                 将HTML的页脚固定在屏幕下方
 * 说明:
 *     处理的方法好像是比较多的,个人还是比较倾向于用JS进行处理。
 *
 *                                   2017-8-25 深圳 龙华樟坑村 曾剑锋
 ********************************************************************/

一、参考文档:
    1. 将footer固定在页面底部的实现方法
        https://segmentfault.com/a/1190000004453249

二、采用代码:
    1. HTML:
        <body>
            <header>header</header>
            <main>main content</main>
            <footer>footer</footer>
        </body>
    2. CSS:
        html,body{margin:0;padding: 0;}
        header{background-color: #ffe4c4;}
        main{background-color: #bdb76b;}
        footer{background-color: #ffc0cb;}

        /* 动态为footer添加类fixed-bottom */
        .fixed-bottom {position: fixed;bottom: 0;width:100%;}
    3. JS:
        $(function(){
            function footerPosition(){
                $("footer").removeClass("fixed-bottom");
                var contentHeight = document.body.scrollHeight, //网页正文全文高度
                    winHeight = window.innerHeight;             //可视窗口高度,不包括浏览器顶部工具栏
                if(!(contentHeight > winHeight)){
                    //当网页正文高度小于可视窗口高度时,为footer添加类fixed-bottom
                    $("footer").addClass("fixed-bottom");
                }
            }
            footerPosition();
            $(window).resize(footerPosition);
        });

 

相关文章:

  • 2021-11-28
  • 2021-12-04
  • 2022-02-04
  • 2021-12-22
  • 2021-04-18
  • 2022-01-17
  • 2021-11-09
  • 2021-07-15
猜你喜欢
  • 2022-12-23
  • 2021-12-04
  • 2021-08-15
  • 2021-12-22
相关资源
相似解决方案