【问题标题】:Transform pseudo-element based on parent根据父元素变换伪元素
【发布时间】:2018-04-26 09:38:08
【问题描述】:

我正在制作一个 Material Design Web 应用程序,并且我制作它以便抽屉只是一个元素,使用 :before 伪元素制作稀松布(使背景变暗并使抽屉突出)。

#app-bar-drawer {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  width: 350px;
  max-width: 85%;
  background: #fff;
  z-index: 1200;
  box-shadow: 0px 8px 10px -5px rgba(0, 0, 0, 0.2), 0px 16px 24px 2px rgba(0, 0, 0, 0.14), 0px 6px 30px 5px rgba(0, 0, 0, 0.12);
  opacity: 0;
  pointer-events: none;
  transform: translateX(-110%);
  transition: opacity .1s, transform .3s;
}

#app-bar-drawer.in {
  opacity: 1;
  pointer-events: all;
  transform: translateX(0%);
}

#app-bar-drawer:before {
  background: rgba(0, 0, 0, 0.6);
  position: absolute;
  width: 10000%;
  transform: translateX(350px);
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  content: "";
  z-index: 700;
}

@media screen and (max-width: 400px) {
  #app-bar-drawer:before {
    transform: translateX(350px); /* this is what I can't figure out */
  }
}

在较大的手机、平板电脑、台式机等上运行良好:

如您所见,稀松布位于抽屉边缘,整齐地覆盖着内容物。但是,对于较小的设备,它会移动得太远,看起来像这样:

在这一个中,稀松布位于屏幕的最右侧边缘,因为transform: translateX(350px) 将其推得太远了。我尝试将其更改为使用百分比,但百分比是 :before 伪元素,而不是父元素。当我尝试使用像 200px 这样的像素度量时,它要么走得太远(并且不会覆盖内容),要么太靠近(并且覆盖抽屉)。

摘要

我不能使用百分比或像素度量。我宁愿在没有 JS 的情况下这样做,但如果有必要我会这样做。这可能吗?它必须是单个元素。

【问题讨论】:

  • 你能分享一下这个问题的最小演示吗?帮助会更容易,谢谢
  • 等等……这需要几分钟
  • 你会考虑在正文中添加伪元素吗?使其全宽并删除转换。确保菜单具有更高的 z-index 并且应该没问题。切换菜单时在 body 上切换一个类。
  • 谢谢,成功了。将其放入答案中,我会将其标记为已接受。

标签: css css-transforms pseudo-element


【解决方案1】:

另一种方法是在切换菜单时切换主体上的伪元素。您可以使用position: fixed 和坐标将其设置为视口的全宽,从而避免处理转换。

确保菜单的z-index 高于伪元素。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-28
    • 2020-10-06
    • 1970-01-01
    相关资源
    最近更新 更多