【问题标题】:Use pseudo-element to overlay a scrolling div使用伪元素覆盖滚动的 div
【发布时间】:2017-08-10 22:07:40
【问题描述】:

我想用伪元素覆盖覆盖具有动态滚动内容的 div。
我遇到的问题是覆盖滚动内容,使折叠下方的任何内容都裸露。

如何让叠加层在其下方的内容滚动时保持原位?

.wantOverlay {
  width: 200px;
  height: 100px;
  overflow-y: scroll;
  position: relative;
}

.wantOverlay::after {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(150, 150, 150, .45);
}
<div class="wantOverlay">
  <div>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean sed est vel ante faucibus tempor nec id.</div>
  <div>Unfortunately any text past this point no longer has the overlay.</div>
  <div>This text no longer has the overlay.</div>
</div>

【问题讨论】:

    标签: css pseudo-element


    【解决方案1】:

    position: sticky; 带有否定的margin-top 可以解决问题。这里是关于stickyhttps://developer.mozilla.org/tr/docs/Web/CSS/position的详细信息

    .wantOverlay {
      width: 200px;
      height: 100px;
      overflow-y: scroll;
      position: relative;
    }
    
    .wantOverlay::after {
      display: block;
      content: ' ';
      position: sticky;
      left: 0;
      top: 0;
      margin-top: -100%;
      width: 100%;
      height: 100%;
      background: rgba(150, 150, 150, .45);
    }
    <div class="wantOverlay">
      <div>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean sed est vel ante faucibus tempor nec id.</div>
      <div>Unfortunately any text past this point no longer has the overlay.</div>
      <div>This text no longer has the overlay.</div>
    </div>

    【讨论】:

    • 经过一些测试,如果文本长度发生变化,这与Syden的回答有类似的问题。
    • 值得注意的是 position:sticky 是一项相当新的功能,可能无法为您提供足够的浏览器支持。 caniuse.com/#search=sticky
    • @Enigmadan 是的,你是对的,用更多的文本对此进行了测试,但它没有用。会努力寻找更好的方法。如果可以的话,我会编辑这个...谢谢你的信息。
    • @Scoots 就我而言,多浏览器支持并不重要,但如果其他人偶然发现这个问题,这一点很重要。
    • sticky 是解决方案!这是以前 hacky 属性的另一种富有成效的组合,但它仍然没有被正确记住......
    【解决方案2】:

    tl.博士。将after 更改为before,删除topleft 属性并继承widthheight 并将position 更改为fixed

    示例后的解释。

    .wantOverlay {
      width: 200px;
      height: 100px;
      overflow-y: scroll;
      position: relative;
    }
    
    .wantOverlay::before {
      content: '';
      position: fixed;
      width: inherit;
      height: inherit;
      background: rgba(150, 150, 150, .45);
    }
    <div class="wantOverlay">
      <div>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean sed est vel ante faucibus tempor nec id.</div>
      <div>Unfortunately any text past this point no longer has the overlay.</div>
      <div>This text no longer has the overlay.</div>
    </div>

    那么,这里发生了什么:
    您想要一个固定的行为,所以我们将位置更改为fixed。这带来了一个缺点,即其他属性将使用窗口作为基础而不是父元素来设置。所以我们修复它,将位置设置为与您的 div 相同的位置,我们删除topleft 属性并将after 更改为before。为了使其大小相同,我们使用inherit,因为100% 将是窗口的100%

    编辑

    警告:我写这篇文章是因为我敢于找到一种使用固定位置的方法。我实际上不认为这是最好的答案,因为它只有在整个页面中没有滚动时才会起作用。如果发生这种情况,灰色方块将始终位于窗口的同一位置,而内容会上下移动。

    如果我要为客户的网站执行此操作,我想我会创建一个包含两个孩子的父 div,即内容和覆盖。如果您想保持滚动功能,您可以在叠加层上使用 css 属性 pointer-events:none;

    【讨论】:

    • 虽然这样可行,但继承宽度也会覆盖滚动条。我的 ui 方面想回避这一点,因为灰色的滚动条可以显示出不难处理。
    • 这是迄今为止唯一完全适用于不同长度文本的响应。对于未来的网络旅行者,您可以使用calc() 减去滚动条的大小,以防止覆盖覆盖它。
    【解决方案3】:

    您可以使用巨大的box-shadow 传播值,以及您想要的背景,例如:

      box-shadow: 0 0 0 10000px rgba(150, 150, 150, .45);
    

    这将涵盖所有内容,但由于box-shadow 无法影响布局,因此不会增加滚动高度。

    注意:这是一种可能会影响性能的 hack,因此请谨慎使用。

    .wantOverlay {
      width: 200px;
      height: 100px;
      overflow-y: scroll;
      position: relative;
    }
    
    .wantOverlay::after {
      content: '';
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 0;
      box-shadow: 0 0 0 10000px rgba(150, 150, 150, .45);
    }
    <div class="wantOverlay">
      <div>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean sed est vel ante faucibus tempor nec id.</div>
      <div>Unfortunately any text past this point no longer has the overlay.</div>
      <div>This text no longer has the overlay.</div>
      <div>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean sed est vel ante faucibus tempor nec id.</div>
      <div>Unfortunately any text past this point no longer has the overlay.</div>
      <div>This text no longer has the overlay.</div>
    </div>

    【讨论】:

    • 我很欣赏这解决了具体问题,但它似乎有点太老套了,无法投入生产。 (特别是当有其他方法时。)我想知道你需要多少像素的 box-shadow 才能使浏览器崩溃......
    • 我不确定当大部分浏览器未呈现时它如何影响浏览器。就我个人而言,我会选择立场粘性的答案。唯一的问题是浏览器支持。
    【解决方案4】:

    根据评论编辑:

    您可以使用展开的内容尺寸固定它。文本可以是动态的。

    .wantOverlay {
      width: 200px;
      height: 100px;
      overflow-y: scroll;
      position: relative;
    }
    
    .wantOverlay::after {
      content: '';
      position: fixed;
      top: 0;
      left: 0;
      background: rgba(150, 150, 150, .45);
      height: 100px;
      margin-top: 8px;
      width: 200px;
    }
    <div class="wantOverlay">
      <div>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean sed est vel ante faucibus tempor nec id.</div>
      <div>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean sed est vel ante faucibus tempor nec id.Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean sed est vel ante faucibus tempor nec id.Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean sed est vel ante faucibus tempor nec id.Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean sed est vel ante faucibus tempor nec id. </div>
      <div>This text no longer has the overlay.</div>
    </div>

    更少的文字:

    .wantOverlay {
      width: 200px;
      height: 100px;
      overflow-y: scroll;
      position: relative;
    }
    
    .wantOverlay::after {
      content: '';
      position: fixed;
      top: 0;
      left: 0;
      background: rgba(150, 150, 150, .45);
      height: 100px;
      margin-top: 8px;
      width: 200px;
    }
    <div class="wantOverlay">
      <div>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean sed est vel ante faucibus tempor nec id.</div>
      <div>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</div>
      <div>This text no longer has the overlay.</div>
    </div>

    【讨论】:

    • 这对我的情况不太适用,因为我的 div 中的文本将是动态的。我不确定会有多少文本,所以硬编码额外的长度不是我能做的。如果这是另一种我知道文本会保持不变的情况,那会很好用。 Calc 很酷!
    • @Enigmadan 抱歉回复晚了,我在上面编辑过,现在应该可以使用动态文本了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-18
    • 2022-01-05
    • 1970-01-01
    • 2017-04-07
    • 2014-03-21
    • 1970-01-01
    相关资源
    最近更新 更多