如何防止两个粘性 <div> 元素重叠?

How to prevent two sticky <div> elements from overlapping?


下面是一个 <div> 元素,它的 position 属性 设置为 sticky:


<div style="position: sticky;"> </div>.


当我在页面中插入两个粘性 <div> 元素时,它们都粘在页面顶部并粘在上面,从这个意义上说,粘性元素在起作用,但是,它们粘在完全相同的位置发现并互相掩盖。在我的脑海里,我想象当用户滚动页面时,它们都会到达页面顶部,然后粘住,但我认为它们会堆叠,但正如我所说,它们不会,一个只是位于另一个下面.

这是我当前项目的一个极其简化的版本。我想让两个方块粘在一起,一个在另一个上面。

<html>
<body>

    <div style="display: block; position: sticky; width: 100% height: 25px; background: #555">
        DIV ONE #1
    </div>

      
    <div style="display: block; position: sticky; width: 100% height: 25px; background: #555">
        DIV TWO #2
    </div>

</body>
</html>

所以我的问题是,如何将两个粘性 <div> 元素添加到同一个 HTML 文档,并拥有一个 <div> 在用户滚动时粘在页面顶部,另一个 <div> 粘在第一个 <div> 的底部,而不是同时粘在页面顶部并覆盖第一个粘住的, 上?


为确保我所说的内容被理解,我添加了一个交互式示例。

下面的示例将向您展示我的项目中发生的事情 — Div Alpha 正在被 Div Beta 覆盖,我希望 Div Beta 粘在 Div Alpha 的底部,这样它不会阻止它。


<!DOCTYPE html>
<html>

<head>
  <style>
    .div-alpha {
      display: block;
      text-align: center;
      position: sticky;
      top: 0;
      width: 200px;
      height: 200px;
      font-size: 30px;
      border: 5px solid #FF20B0;
      background-color: #000000;
      color: #FF20B0;
    }
    
    .div-beta {
      display: block;
      text-align: center;
      position: sticky;
      top: 0;
      width: 200px;
      height: 200px;
      font-size: 30px;
      border: 5px solid #80E000;
      background-color: #002040;
      color: #80E000;
    }
    
    h1 {
      color: #401480;
    }
    
    p.lorem-ipsum {
      width: 350px;
      font-size: 18px;
      color: #001064
    }
    
    p.p-alpha {
      font-size: 14px;
      color: #FF20B0;
    }
    
    p.p-beta {
      font-size: 14px;
      color: #80E000;
    }
  </style>

</head>

<body>

  <h1>Testing Sticky Divs</h1>
  ---

  <br>

  <div class="div-alpha">
    DIV ALPHA
    <p class="p-alpha">The other div covers me up, and I don't want to be covered up!</p>
  </div>

  <br>

  <div class="div-beta">
    DIV BETA
    <p class="p-beta"> I don't want to cover the other div, but I do anyway :..(</p>
  </div>

<!-- The Code Below is silly filler code that has been inserted so that the page will scroll up & down, which is required for observing the behavior of elements that have their "position" property set to "sticky" (i.e. "position: sticky;") -->

  <br>
  <br>
  <br>
  <br>

  <h2>Lorem Ipsum Text</h2>

  ---
  <p class="lorem-ipsum">
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Mi ipsum faucibus vitae aliquet nec. Tempus quam pellentesque nec nam aliquam. Purus non enim praesent elementum facilisis leo
    vel fringilla est. Mattis ullamcorper velit sed ullamcorper morbi tincidunt. Eu consequat ac felis donec et odio pellentesque. In ante metus dictum at tempor commodo. Amet massa vitae tortor condimentum. Sapien eget mi proin sed libero enim sed faucibus
    turpis. Tortor at risus viverra adipiscing at. Leo urna molestie at elementum eu facilisis sed. Pharetra diam sit amet nisl suscipit adipiscing. Cursus sit amet dictum sit amet justo donec. Euismod nisi porta lorem mollis. Massa ultricies mi quis
    hendrerit. Lorem ipsum dolor sit amet consectetur. Facilisi etiam dignissim diam quis enim lobortis scelerisque fermentum dui. Mi in nulla posuere sollicitudin aliquam ultrices sagittis. Ornare arcu odio ut sem nulla pharetra. Faucibus et molestie
    ac feugiat sed lectus. Commodo quis imperdiet massa tincidunt nunc. At augue eget arcu dictum varius duis. Potenti nullam ac tortor vitae purus faucibus ornare suspendisse sed. Et molestie ac feugiat sed lectus vestibulum mattis ullamcorper. Convallis
    posuere morbi leo urna molestie at. Enim sit amet venenatis urna cursus eget nunc scelerisque viverra. Tristique senectus et netus et malesuada fames ac. Faucibus ornare suspendisse sed nisi lacus sed viverra tellus. Ut aliquam purus sit amet luctus
    venenatis lectus. Posuere urna nec tincidunt praesent. Aenean et tortor at risus viverra adipiscing at in. Justo eget magna fermentum iaculis eu. Placerat vestibulum lectus mauris ultrices eros in. Pharetra vel turpis nunc eget lorem dolor. Blandit
    turpis cursus in hac habitasse platea dictumst quisque. Nisi porta lorem mollis aliquam ut porttitor leo. Lectus nulla at volutpat diam ut venenatis. Proin nibh nisl condimentum id venenatis. Arcu felis bibendum ut tristique et egestas quis ipsum.
    Feugiat nibh sed pulvinar proin gravida. Odio facilisis mauris sit amet. Gravida in fermentum et sollicitudin ac. Magna etiam tempor orci eu lobortis elementum nibh. Donec ultrices tincidunt arcu non sodales. Consequat ac felis donec et odio. Amet
    mattis vulputate enim nulla aliquet porttitor lacus luctus. Sagittis purus sit amet volutpat consequat mauris nunc. Id interdum velit laoreet id donec ultrices tincidunt arcu non. Diam sit amet nisl suscipit. Viverra tellus in hac habitasse platea
    dictumst vestibulum. Praesent tristique magna sit amet purus gravida.
  </p>

</body>

</html>

这是一个例子

div.sticky {
  position: -webkit-sticky;
  position: sticky;
  top: 0;
  background-color: yellow;
  padding: 50px;
  font-size: 20px;
}

<div class="sticky">
<p> This is your sticky box </p>
</div>
<div>
<p>This is your other divs and properties </p>
</div>
所以我想通了:

坚持 2 个 Div,w/o 互相覆盖


您可以通过两种方式配置 Sticky <div> 元素,使它们在您向下滚动页面时不会相互覆盖。


#1


第一种方式是将下方div的属性top设置为与顶部相同的组合高度div。这里的关键字是 COMBINED 意思是:padding 和 borders 需要添加到高度以获得 top 的准确值,否则 divs仍将部分覆盖彼此。


#2

最简单、直接的方法是创建一个粘性父 div,然后将两个原始 div 放入其中。从原来的两个 <div> 元素中删除 position: sticky; 属性 ,这样 position 仍然设置为默认值。重要的是,在执行此操作时,请确保只有父容器的位置 属性 设置为粘性(即 position: sticky),否则您将得到不想要的结果。下面是使用 solution #2.

重写的问题代码

<!DOCTYPE html>
<html>

<head>
    <style>
        .div-alpha {
            display: block;
            text-align: center;
            width: 200px;
            height: 200px;
            font-size: 30px;
            text-decoration: underline;
            border: 5px solid #08C8FF;
            background-color: #900040;
            color: #08C8FF;
        }

        .div-beta {
            display: block;
            text-align: center;
            width: 200px;
            height: 200px;
            font-size: 30px;
            text-decoration: underline;
            border: 5px solid #EE1054;
            background-color: #00307A;
            color: #EE1054;
        }

        .div-gamma {
            display: block;
            text-align: center;
            position: sticky;
            top: 0;
        }

        p {
            width: 350px;
            font-size: 18px;
        }
    </style>

</head>

<body>

    <h1>Testing Sticky Divs</h1>
    ---

    <br>

    <div class="div-gamma">
        <div class="div-alpha">DIV ALPHA</div>
        <div class="div-beta">DIV BETA</div>
    </div>

    <br>
    <br>
    <br>
    <br>

    <h3>Lorem Ipsum Text</h3>
    ---
    <p>
        Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore
        magna aliqua. Mi ipsum faucibus vitae aliquet nec. Tempus quam pellentesque nec nam aliquam. Purus non enim
        praesent elementum facilisis leo vel fringilla est. Mattis ullamcorper velit sed ullamcorper morbi tincidunt. Eu
        consequat ac felis donec et odio pellentesque. In ante metus dictum at tempor commodo. Amet massa vitae tortor
        condimentum. Sapien eget mi proin sed libero enim sed faucibus turpis. Tortor at risus viverra adipiscing at.
        Leo urna molestie at elementum eu facilisis sed. Pharetra diam sit amet nisl suscipit adipiscing. Cursus sit
        amet dictum sit amet justo donec. Euismod nisi porta lorem mollis. Massa ultricies mi quis hendrerit. Lorem
        ipsum dolor sit amet consectetur. Facilisi etiam dignissim diam quis enim lobortis scelerisque fermentum dui.

        Mi in nulla posuere sollicitudin aliquam ultrices sagittis. Ornare arcu odio ut sem nulla pharetra. Faucibus et
        molestie ac feugiat sed lectus. Commodo quis imperdiet massa tincidunt nunc. At augue eget arcu dictum varius
        duis. Potenti nullam ac tortor vitae purus faucibus ornare suspendisse sed. Et molestie ac feugiat sed lectus
        vestibulum mattis ullamcorper. Convallis posuere morbi leo urna molestie at. Enim sit amet venenatis urna cursus
        eget nunc scelerisque viverra. Tristique senectus et netus et malesuada fames ac. Faucibus ornare suspendisse
        sed nisi lacus sed viverra tellus. Ut aliquam purus sit amet luctus venenatis lectus. Posuere urna nec tincidunt
        praesent. Aenean et tortor at risus viverra adipiscing at in. Justo eget magna fermentum iaculis eu. Placerat
        vestibulum lectus mauris ultrices eros in.

        Pharetra vel turpis nunc eget lorem dolor. Blandit turpis cursus in hac habitasse platea dictumst quisque. Nisi
        porta lorem mollis aliquam ut porttitor leo. Lectus nulla at volutpat diam ut venenatis. Proin nibh nisl
        condimentum id venenatis. Arcu felis bibendum ut tristique et egestas quis ipsum. Feugiat nibh sed pulvinar
        proin gravida. Odio facilisis mauris sit amet. Gravida in fermentum et sollicitudin ac. Magna etiam tempor orci
        eu lobortis elementum nibh. Donec ultrices tincidunt arcu non sodales. Consequat ac felis donec et odio. Amet
        mattis vulputate enim nulla aliquet porttitor lacus luctus. Sagittis purus sit amet volutpat consequat mauris
        nunc. Id interdum velit laoreet id donec ultrices tincidunt arcu non. Diam sit amet nisl suscipit. Viverra
        tellus in hac habitasse platea dictumst vestibulum. Praesent tristique magna sit amet purus gravida.
    </p>

</body>

</html>

这就是我制作具有功能响应式移动下拉菜单的导航栏的方法。听起来你已经想通了,但我想我会给你一些反馈。然而,从表面上看,范例是将所有应该粘在一个粘性容器中的对象放在一起;实施它比听起来要难得多。祝你好运!


<!DOCTYPE HTML>
<html lang='us-en'>
<head>
<style type='text/css'>
.nav {
      position: sticky;
      position: -webkit-sticky;
      top: 0;
      left: 0;
      display: grid;
      grid-template-columns: 100%;
      width: 100%;
      margin: 0;
      padding: 0;
}

.nav-bar {
      background-color: #000;
      display: block;
      width: 100%;
}

.nav-bar a {
      display: inline-block;
      font-size: 26px);
      text-decoration: none;
      margin: 16px 4px 0 12px;
}



/*!!! ~~~ ICONS ~~~ */
#home {
      display: block;
      float: left;
      padding: 12px;
      font-size: 38px !important;

}

#bars {
      display: none;
      float: right;
      padding: 4px;
      font-size: 38px !important;
}



/*! ~~~ Drop & Drop-Items  ~~~ */
.nav-drop {
      background-color: #000;
      display: none;
      width: 100%;
}

.nav-drop button {
      display: block;
      width: 54%;
      margin: 12px 23%;
      border: 1px solid #0FF;
      padding: 1px;
}

</style>
</head>


<!-- BODY'S MARKUP -->
<body>

<div class="nav">
      <div class="nav-bar">
            <i id="home" class="fa fa-home" aria-hidden="true" onclick="go2('home')">&nbsp; </i>
            <a href="home">HOME &nbsp;| </a>
            <a href="about">ABOUT &nbsp;| </a>
            <a href="contact">CONTACT &nbsp;| </a>
            <a href="forum">FORUM</a>
            <i id="bars" class="fa fa-bars" aria-hidden="true" onclick="dropMenu()"></i>
      </div>
      <div id="nav-drop" class="nav-drop">
            <button onclick="go2('about')">ABOUT</button>
            <button onclick="go2('contact')">CONTACT</button>
            <button onclick="go2('forum')">FORUM</button>
      </div>
</div>
</body>
</html>