代码之家  ›  专栏  ›  技术社区  ›  Bharat

如何使粘贴的横幅保持在页脚上方?

  •  0
  • Bharat  · 技术社区  · 7 年前

    在我的网站上,我必须创建一个策略横幅。

    我的网站已经包含内容和页脚的div。

    我想要什么?

    1. 当用户在页面中滚动时,横幅应该在滚动时贴在底部,当页脚出现时,横幅应该停止。

    我的示例代码:

    .main-area {
      height: 1000vh;
      background-color: #ccc;
      width: 2000px;    
      position: relative;
    }
    
    .policy-banner {
      height: 100px;
      width: 2000px;
      color: yellow;
      background-color: blue;
      position: fixed;
      bottom: 0;
      left: 0;
    }
    
    .footer {
      background-color: black;
      height: 50px;
      color: white;
      position: fixed;
      bottom: 0;
      left: 0;
    }
    <html>
    <head>
    </head>
    <body>
      <div class="main-area">
        <section>another sections coming dynamically</section>
        <section>another sections coming dynamically</section>
    
        <div class="policy-banner">
          this is banner, it should stick to the user's view port. 
          when user scroll, it will also scroll but upto footer and should not overlap with footer.
        </div>
      </div>
    
      <footer class="footer">
        this is footer. it will stay at the end of the page.
      </footer>
    </body>
    </html>

    任何帮助都将不胜感激。谢谢

    2 回复  |  直到 7 年前
        1
  •  5
  •   SourceOverflow    7 年前

    你想要的,是拥有 position: sticky; 而不是 fixed . 这意味着元素在达到条件之前表现为相对定位,然后在条件完全填充期间表现为固定。

    bottom: 0; ,因此它将开始修复,因为您从其父div开始 .main-area ,但一旦超出父分区,请停止。

    .main-area {
      background-color: #ccc;
    }
    
    .main-area .empty {
      height: 500px;
      background-color: #ddd;
    }
    
    .policy-banner {
      height: 100px;
      color: yellow;
      background-color: blue;
      position: -webkit-sticky;
      position: sticky;
      bottom: 0;
    }
    
    .footer {
      background-color: black;
      height: 50px;
      width: 100%;
      color: white;
    }
    <html>
    <head>
    </head>
    <body>
      <div class="main-area">
        <div class="empty">Section 1</div>
        <div class="empty">Section 2</div>
        <div class="policy-banner">
          this is banner, it should stick to the user's view port. 
          when user scroll, it will also scroll but upto footer and should not overlap with footer.
        </div>
      </div>
    
      <footer class="footer">
        this is footer. it will stay at the end of the page.
      </footer>
    </body>
    </html>
        2
  •  0
  •   Hussnain Ali Hamza    7 年前

    是的,这很容易做到,

    您可以做的是,使用javascript函数注册您的页脚,当页脚出现在用户视口中时,会调用该函数,请查看此帖子,

    How to Check if element is visible after scrolling?