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

CSS动画覆盖

  •  1
  • kperveen  · 技术社区  · 7 年前

    div.relative {
      position: relative;
      width: 400px;
      height: 200px;
      border: 3px solid #73AD21;
    }
    
    div.absolute {
      position: absolute;
      top: 50%;
      right: 0;
      width: 200px;
      height: 100px;
      border: 3px solid #73AD21;
      opacity: 1;
      background-color: blue;
    }
    
    .overlay {
      position: fixed;
      top: 0;
      bottom: 0;
      left: 0;
      right: 0;
      background: rgba(0, 0, 0, 0.95);
      color: white;
      z-index: 1;
    }
    
    .div1 {
      animation: 750ms 1 forwards ani;
    }
    
    @keyframes ani {
      0% {
        opacity: 0;
      }
      100% {
        opacity: 1;
      }
    }
    <h2>
      position: absolute;</h2>
    
    <p>An element with position: absolute; is positioned relative to the nearest positioned ancestor (instead of positioned relative to the viewport, like fixed):</p>
    
    <div class="relative">
      This div element has position: relative;
      <div class="div1">
        This is a div
        <div class="overlay">
          This div element has position: fixed;
        </div>
      </div>
    
      <div class="absolute">
        This div element has position: absolute;
      </div>
    </div>

    我正在尝试创建一个覆盖整个区域的覆盖图。但是,问题是,当我添加动画时,它会将绝对元素带到前面,尽管动画没有应用到它

    3 回复  |  直到 7 年前
        1
  •  2
  •   Erwin Sanders    7 年前

    <div class="relative">
      This div element has position: relative;
      <div class="div1">
        This is a div
      </div>
    
      <div class="absolute">
        This div element has position: absolute;
      </div>
    </div>
    <div class="overlay">
      This div element has position: fixed;
    </div>
    
        2
  •  0
  •   Erwin Sanders    7 年前

    既然我的最后一个答案不是最佳实践,那就试试这个吧。 因此,将这些样式添加到具有类div1的div中:

    .div1 {
      animation: 750ms 1 forwards ani;
      position: relative;
      z-index: 2;
    }
    

    z索引不起作用,因为定位的绝对元素与固定元素位于不同的级别,您需要定义相同级别的元素(在本例中为div1和absolute)的z索引。z索引还需要有位置相对属性才能工作。。

        3
  •  0
  •   kperveen    7 年前

    (对每个寻找答案的人)我发现了这个代码的问题。当我们应用值小于1的不透明度动画时,它会为该特定元素及其子元素创建另一个堆叠上下文,并根据该堆叠上下文堆叠覆盖。我通过将动画填充模式移除为“无”来解决这个问题,因为之后它不会影响任何CSS。