三行代码  ›  专栏  ›  技术社区  ›  Dshiz

无法将此页脚正确固定到视口底部

  •  0
  • Dshiz  · 技术社区  · 1 月前

    我正在尝试自动滚动的内容,这是工作。但是,当我尝试将页脚固定到视口底部时,内容会滚动经过页脚,页脚会与内容一起滚动。

    如果我删除页脚元素的CSS,页脚将从顶部开始,内容将其推到底部,并且内容将按预期自动滚动到页脚上方。但页脚并不像我希望的那样固定在底部。

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <title>hyperbole</title>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" href="https://cdn.rawgit.com/Chalarangelo/mini.css/v3.0.1/dist/mini-default.min.css">
    </head>
    <style>
        #content{
            height: 100%;
            display: flex;
            flex-direction: column;
            overflow-y: scroll;
        }
        footer {
            position: absolute !important;
            bottom: 0 !important;
            width: 100%;
        }
    
    
    </style>
    <body>
        <div class="container" id="cont">
            <header class="sticky"><h1>Header</h1></header>
            <div id="content"></div>
            <footer class="sticky"><h3>Footer</h3></footer>
        </div>
    </body>
    <script>
        window.addEventListener('DOMContentLoaded', (event) => {
    
            history.scrollRestoration = "manual"
            window.scrollTo(0, document.body.scrollHeight)
    
    
            setInterval(()=>{
    
                var content = document.getElementById("content")
                var textnode = document.createElement("div")
    
                textnode.innerText = Math.random()
                content.appendChild(textnode)
    
    
                window.scrollTo(0, document.body.scrollHeight)
    
            }, 500)
    
    
        })
    
    </script>
    <html>
    
    2 回复  |  直到 1 月前
        1
  •  2
  •   Programmer    1 月前

    你需要使用 position: fixed position: absolute . 位置:绝对位置 将元素从文档流中取出,并将其相对于 整页 . 还将其从文档流中取出,但将其相对于视口放置。您将再次在页脚下隐藏内容-要解决此问题,请设置一个 margin-bottom body

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <title>hyperbole</title>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" href="https://cdn.rawgit.com/Chalarangelo/mini.css/v3.0.1/dist/mini-default.min.css">
    </head>
    <style>
        #content{
            height: 100%;
            display: flex;
            flex-direction: column;
            overflow-y: scroll;
        }
        footer {
            position: fixed !important;
            bottom: 0 !important;
            width: 100%;
        }
        
        body {
            margin-bottom: 80px;
        }
    </style>
    <body>
        <div class="container" id="cont">
            <header class="sticky"><h1>Header</h1></header>
            <div id="content"></div>
            <footer class="sticky"><h3>Footer</h3></footer>
        </div>
    </body>
    <script>
        window.addEventListener('DOMContentLoaded', (event) => {
    
            history.scrollRestoration = "manual"
            window.scrollTo(0, document.body.scrollHeight)
    
    
            setInterval(()=>{
    
                var content = document.getElementById("content")
                var textnode = document.createElement("div")
    
                textnode.innerText = Math.random()
                content.appendChild(textnode)
    
    
                window.scrollTo(0, document.body.scrollHeight)
    
            }, 500)
    
    
        })
    
    </script>
    <html>

        2
  •  1
  •   GenericUser    1 月前

    将页脚设置为 fixed !important;

    window.addEventListener('DOMContentLoaded', (event) => {
    
      history.scrollRestoration = "manual";
      window.scrollTo(0, document.body.scrollHeight);
    
      const content = document.getElementById("content");
    
      setInterval(() => {
        const textnode = document.createElement("div");
    
        textnode.innerText = Math.random();
        content.appendChild(textnode);
    
        window.scrollTo(0, document.body.scrollHeight);
    
      }, 500);
    });
    #content {
      position: absolute;
      height: 100%;
      display: flex;
      flex-direction: column;
      overflow-y: scroll;
      width: calc(100% - 20px);
    }
    
    footer {
      position: fixed !important;
      bottom: 0 !important;
      width: 100%;
    }
    <!DOCTYPE html>
    <html lang="en">
    
    <head>
      <title>hyperbole</title>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <link rel="stylesheet" href="https://cdn.rawgit.com/Chalarangelo/mini.css/v3.0.1/dist/mini-default.min.css">
    </head>
    
    <body>
      <div style="position:relative; height:calc(100vh - 140px)">
        <div class="container" id="cont">
          <header class="sticky">
            <h1>Header</h1>
          </header>
          <div id="content"></div>
          <footer class="sticky">
            <h3>Footer</h3>
          </footer>
        </div>
      </div>
    </body>