动画元素在 Firefox 的父容器外不可见

Animated element not visible outside of parent container in Firefox

我有一个动画 div 飞到视口的右上角。

但是,由于 overflow 属性,它在 Firefox 的父容器之外不可见。它在 Chrome.

中完全可见

Firefox 中滚动条后面的元素:

元素在 Chrome 的父元素之上:

如何让它在 Firefox 中也能正常工作?如果从 .container 中删除 overflow-y: auto,问题就不会再出现,但这不是一个可行的解决方案,因为我需要可滚动的内容。

这是一个例子。您可以检查它是否在 Chrome 中产生了所需的行为,但在 Firefox 中却没有:

.app {
  overflow: hidden;
}

.container {
  width: 260px;
  max-height: 400px;
  background: blue;
  left: 10px;
  right: 10px;
  top: 10px;
  position: fixed;
  z-index: 500;
  overflow-y: auto;
}


.wrapper {
  height: 250px;
  padding: 10px;
  margin: 5px;
  background: yellow;
  top: 5px;
  position: sticky;
}

.content {
  height: 600px;
  margin: 5px;
  background: orange;
}



@keyframes fly-to-top {
  10% {
    top: 150px;
    right: 80%;
    width: 50px;
  }

  30% {
    top: 120px;
    right: 70%;
    width: 45px;
  }

  60% {
    top: 75px;
    right: 40%;
    width: 40px;
  }

  100% {
    top: 10px;
    right: 160px;
    width: 35px;
  }
}


.animated {
  position: fixed;
  right: unset;
  top: 165px;
  width: 50px;
  background: red;
  color: white;
  animation: fly-to-top linear 2s forwards;
  display: flex;
  align-items: flex-start;
}
<div class="app">
  <div class="container">
    <div class="wrapper">
      <div class="animated">
        Text
      </div>
    </div>
    <div class="content">
      Lorem ipsum
    </div>
  </div>
</div>

评论后编辑:

您可以在 HTML 代码的更高级别上将动画元素从其父元素(即具有 overflow: hidden 的元素)中取出 - 作为容器的兄弟元素。我在下面的代码片段中这样做了,还添加了一个 z-index 将动画元素放在容器上方:

.app {
  overflow: hidden;
}

.container {
  width: 260px;
  max-height: 400px;
  background: blue;
  left: 10px;
  right: 10px;
  top: 10px;
  position: fixed;
  z-index: 500;
  overflow-y: auto;
}

.wrapper {
  height: 250px;
  padding: 10px;
  margin: 5px;
  background: yellow;
  top: 5px;
  position: sticky;
}

.content {
  height: 600px;
  margin: 5px;
  background: orange;
}

@keyframes fly-to-top {
  10% {
    top: 150px;
    right: 80%;
    width: 50px;
  }
  30% {
    top: 120px;
    right: 70%;
    width: 45px;
  }
  60% {
    top: 75px;
    right: 40%;
    width: 40px;
  }
  100% {
    top: 10px;
    right: 160px;
    width: 35px;
  }
}

.animated {
  position: fixed;
  right: unset;
  top: 165px;
  width: 50px;
  background: red;
  color: white;
  animation: fly-to-top linear 2s forwards;
  display: flex;
  align-items: flex-start;
  z-index: 501;
}
<div class="app">
  <div class="container">
    <div class="wrapper">
    </div>
    <div class="content">
      Lorem ipsum
    </div>
  </div>
  <div class="animated">
    Text
  </div>
</div>

试试这个分裂:

  1. .wrapper位置设置为固定
  2. .content 下移 transform: translateY()
  3. .wrapperclass中,我添加了pointer-events: none;,因为 如果光标在没有 属性 的 .wrapper 块上,鼠标 滚轮无法滚动内容,只有在拖动时才能滚动 滚动条。

.app {
  overflow: hidden;
}

.container {
  width: 260px;
  max-height: 400px;
  background: blue;
  left: 10px;
  right: 10px;
  top: 10px;
  position: fixed;
  z-index: 500;
  overflow-y: auto;
}

.wrapper {
  display: flex;
  height: 250px;
  padding: 10px;
  margin: 5px;
  background: yellow;
  /* top: 5px; */

  position: fixed; /* changed */
  /* calculate '.container' width - scroll-track-width(12px-17px) - '.wrapper' padding(left, right) - margin(left, right) */
  width: calc(260px - 12px - 20px - 10px);
  z-index: 5;
  pointer-events: none; /* mouse wheel work with this property */
}

.content {
  height: 600px;
  margin: 5px;
  background: orange;
  /* calculate '.wrapper' properties to shift '.content' down  */
  /* height + padding(top, bottom) + margin-bottom */
  transform: translateY(calc(250px + 20px + 5px));
}

@keyframes fly-to-top {
  10% {
    top: 150px;
    right: 80%;
    width: 50px;
  }

  30% {
    top: 120px;
    right: 70%;
    width: 45px;
  }

  60% {
    top: 75px;
    right: 40%;
    width: 40px;
  }

  100% {
    top: 10px;
    right: 160px;
    width: 35px;
  }
}

.animated {
  position: fixed;
  right: unset;
  top: 165px;
  width: 50px;
  background: red;
  color: white;
  animation: fly-to-top linear 2s forwards;
  display: flex;
  align-items: flex-start;
  z-index: 100;
}
<div class="app">
  <div class="container">
    <div class="wrapper">
      <div class="animated">
        Text
      </div>
    </div>
    <div class="content">
      Lorem ipsum
    </div>
  </div>
</div>