【问题标题】:Animated element not visible outside of parent container in Firefox在 Firefox 中,动画元素在父容器之外不可见
【发布时间】:2022-01-19 23:17:07
【问题描述】:

我有一个动画 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 css firefox css-animations css-position


    【解决方案1】:

    试试这个解决方案:

    1. .wrapper 位置设置为固定
    2. .content 向下移动 transform: translateY()
    3. .wrapper 类中,我添加了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>

    【讨论】:

    • 感谢您的解决方案。您是否知道 Chrome 和 Firefox 行为不同的原因是什么?
    • Chrome 使用 Blink 引擎,而 Firefox 使用 Gesko / Quantum 引擎,在这种情况下,我们有时会遇到不同渲染视图的问题帆布。否则可能是 Firefox 或 Chrome 的 bug,你可以到官方论坛询问这个问题。
    【解决方案2】:

    评论后编辑:

    您可以将动画元素从其父元素(即具有overflow: hidden 的元素)中取出,在 HTML 代码的更高级别 - 作为容器的兄弟。我在下面的 sn-p 中这样做了,还添加了一个 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>

    【讨论】:

    • 抱歉,我的帖子似乎不够清楚。我想要元素飞出父容器到视口的右上角。但是,在 Firefox 中,该元素在父容器之外是不可见的。我会努力改进我的帖子。
    • 请注意,我根据我为回复您的评论而写的评论编辑了我的答案,包括 sn-p。
    • 非常感谢。这是一个有效的解决方案,尽管遗憾的是它不是最实用的。我希望找到一种方法使现有设置在 Firefox 中运行,因为它在 Chrome 中运行良好。
    • 我最终还是这样做了——尽管我使用了 react so 并使用了React.Portal,它允许我将元素放置在代码中我想要的位置。但是,我将赏金授予另一个答案,因为它更接近我的要求。
    猜你喜欢
    • 2018-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-29
    • 1970-01-01
    • 2020-06-07
    • 2014-12-27
    相关资源
    最近更新 更多