温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

网站footer沉底效果的三种解决方案

发布时间:2021-06-26 11:09:31 来源:亿速云 阅读:147 作者:小新 栏目:web开发

小编给大家分享一下网站footer沉底效果的三种解决方案,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

问题背景

很多网站设计一般是两个部分,content + footer,content里面装的是网站主体内容,footer里面展示网站的注册信息等等,因为网站内容高度不定的原因,会出现下面两种情况:

1.内容较少时,这个footer固定在在页面的底部。如下所示:

网站footer沉底效果的三种解决方案

2.内容较长时,footer跟在内容后面滑动,大致表现如下图红色框起来的部分:

网站footer沉底效果的三种解决方案

这个需求在PC端还是很常见的,我在自己的应用中也遇到了这个问题,今天总结了一下实现这种布局的几个方法。

方法1 使用js计算

为什么第一个就采用js控制的呢,因为实不相瞒,当初我第一次遇到这个问题的时候,直接就使用js去解决的(主要是我知道js肯定能实现的,所以也就没有花时间去想别的方法)

主要思路是:在页面加载完成后计算屏幕高度 - content内容真实的高度的值,如果差值大于

footer的高度,就给footer的style加上fixed定位,使它固定在屏幕底部。

demo代码如下:

<!DOCTYPE html>
<html>
<head>
    <title>footer沉底效果</title>
    <style type="text/css">
        div {
            margin: 0,
            padding: 0;
            box-sizing: border-box;
            position: relative;
        }
        html, body {
            width: 100%;
            height: 100%;
        }
        #container {
            width: 100%;
            height: 100%;
        }
        #content {
            background: blue;
        }
        #footer {
            width: 100%;
            height: 100px;
            background: red;
        }
        .footer-fixed {
            position: fixed;
            left: 0;
            bottom: 0;
        }
    </style>
</head>
<body>

<div id="container">
    <div id="content"> content </div>

    <div id="footer">
        footer
    </div>
</div>

<script type="text/javascript">
    let height = document.getElementById('container').clientHeight - document.getElementById('content').clientHeight;
    // 这里给footer加上另外的class,使其固定
    if (height > 100) document.getElementById('footer').classList.add('footer-fixed');
</script>

</body>
</html>

本着能使用css解决就绝对不使用js的原则,这个方法虽然最容易想到,但是还是不推荐使用,而且,这段css代码要获取clientHeight,将会导致页面页面重排和重绘,性能考虑上来说,也不推荐。

方法2 采用flex布局 + min-height

flex布局中的justify-content: space-between;搭配超级好用的min-height,刚好可以满足在content内容不足的时候,footer的沉底效果

demo代码如下:

<!DOCTYPE html>
<html>
<head>
    <title>footer沉底效果</title>
    <style type="text/css">
        div {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            position: relative;
        }
        html, body {
            width: 100%;
            height: 100%;
            margin: 0;
            padding: 0;
        }
        #container {
            width: 100%;
            // 重点代码
            // 虽然不知道container的高度,但是可以设置一个最小高度,这样有利于布局
            min-height: 100%;
            display: flex;
            flex-direction: column;
            justify-content: space-between;
        }
        #content {
            background: blue;
        }
        #footer {
            width: 100%;
            height: 100px;
            background: red;
        }
    </style>
</head>
<body>

<div id="container">
    <div id="content"> 
        content  <br>
        content  <br>
        content  <br>
        content  <br>
        content  <br>
        content  <br>
        content  <br>
        content  <br>
        content  <br>
        content  <br>
        content  <br>
        content  <br>
        content  <br>
        content  <br>
        content  <br>
        content  <br>
        content  <br>
    </div>

    <div id="footer">
        footer
    </div>
</div>

</body>
</html>

min-height实在是超级好用的一个css属性了,搭配flex轻松实现沉底效果。

方法3 巧用flex + margin-top

这个技巧是在讲margin auto的妙用中学到的,在flex格式化上下文中,margin auto会自动去分配剩余空间。这里面我们可以在footer上使用margin-top:auto来达到沉底效果。

<!DOCTYPE html>
<html>
<head>
    <title>footer沉底效果</title>
    <style type="text/css">
        div {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            position: relative;
        }
        html, body {
            width: 100%;
            height: 100%;
            margin: 0;
            padding: 0;
        }
        #container {
            width: 100%;
            min-height: 100%;
            display: flex;
            flex-direction: column;
        }
        #content {
            background: blue;
        }
        #footer {
            width: 100%;
            height: 100px;
            background: red;
            margin-top: auto; // 重点代码
        }
    </style>
</head>
<body>

<div id="container">
    <div id="content"> 
        content  <br>
        content  <br>
        content  <br>
        content  <br>
        content  <br>
        content  <br>
        content  <br>
        content  <br>
        content  <br>
        content  <br>
        content  <br>
        content  <br>
        content  <br>
        content  <br>
        content  <br>
        content  <br>
        content  <br>
        content  <br>
        content  <br>
        content  <br>
        content  <br>
        content  <br>
        content  <br>
        content  <br>
        content  <br>
        content  <br>
        content  <br>
        content  <br>
        content  <br>
        content  <br>
        content  <br>
        content  <br>
        content  <br>
        content  <br>
    </div>

    <div id="footer">
        footer
    </div>
</div>

</body>
</html>

以上是“网站footer沉底效果的三种解决方案”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI