footer高度任意+js实现footer在底部

Posted 黎波波

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了footer高度任意+js实现footer在底部相关的知识,希望对你有一定的参考价值。

<body> <header>header</header>
    <main>main content</main>
    <footer>footer</footer>
</body>

/* 动态为footer添加类fixed-bottom */
.fixed-bottom {position: fixed;bottom: 0;width:100%;}

 

$(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");
        } else {
            $("footer").removeClass("fixed-bottom");
        }
    }
    footerPosition();
    $(window).resize(footerPosition);
});

 



















以上是关于footer高度任意+js实现footer在底部的主要内容,如果未能解决你的问题,请参考以下文章

如何让footer始终在页面底部的方法

sticky footer绝对底部的2种实现方法

Sticky Footer的实现

底部粘连(stiky footer)布局

内容不超过屏幕,footer固定在底部,超过时被撑出屏幕

让footer始终待在页面底部