简体   繁体   English

是否可以使用 css 在水平可滚动内容上放置叠加 div - 没有 JS

[英]Is it possible to position overlay div on a horizontal scrollable content with css - no JS

I have div with horizontal scrollable content.我有带有水平可滚动内容的 div。 Trying to apply overlay for the entire content just using css.尝试仅使用 css 为整个内容应用叠加层。 This scrollable content may keep growing.这种可滚动的内容可能会不断增长。 Is there a way to achieve this without javascript code.有没有办法在没有 javascript 代码的情况下实现这一点。

 .bg { height: 50px; width: 50px; display: flex; } .container { display: flex; overflow-x: auto; width: 300px; align-items: flex-start; position: relative; flex-direction: row; } .overlay { position: absolute; background-color: #888888; top: 0; opacity: 0.8; bottom: 0; right: 0; left: 0; }
 <div class="container"> <div class="overlay"> </div> <img src="https://opensource.org/files/osi_keyhole_300X300_90ppi_0.png" class="bg"/> <img src="https://opensource.org/files/osi_keyhole_300X300_90ppi_0.png" class="bg"/> <img src="https://opensource.org/files/osi_keyhole_300X300_90ppi_0.png" class="bg"/> <img src="https://opensource.org/files/osi_keyhole_300X300_90ppi_0.png" class="bg"/> <img src="https://opensource.org/files/osi_keyhole_300X300_90ppi_0.png" class="bg"/> <img src="https://opensource.org/files/osi_keyhole_300X300_90ppi_0.png" class="bg"/> <img src="https://opensource.org/files/osi_keyhole_300X300_90ppi_0.png" class="bg"/> <img src="https://opensource.org/files/osi_keyhole_300X300_90ppi_0.png" class="bg"/> <img src="https://opensource.org/files/osi_keyhole_300X300_90ppi_0.png" class="bg"/> </div>

try this尝试这个

Using the overlay effect by flex property instead of absolute positioning .通过flex 属性使用覆盖效果而不是绝对定位

.overlay{
  white-space:nowrap;  display: flex;
  align-items: flex-start;
  flex-direction: row;
  background-color: #888888;
  opacity: 0.8;
}

And add the images inside the overlay div并在叠加 div 内添加图像

<div class="container">
  <div class="overlay">
    <-- images here --->
  </div>
</div>

 .bg { height: 50px; width: 50px; display: flex; } .container { display: flex; overflow-x: auto; width: 300px; align-items: flex-start; position: relative; flex-direction: row; } .overlay{ white-space:nowrap; display: flex; align-items: flex-start; flex-direction: row; background-color: #888888; opacity: 0.8; } .images img{ white-space: normal;}
 <div class="container"> <div class="overlay"> <img src="https://opensource.org/files/osi_keyhole_300X300_90ppi_0.png" class="bg"/> <img src="https://opensource.org/files/osi_keyhole_300X300_90ppi_0.png" class="bg"/> <img src="https://opensource.org/files/osi_keyhole_300X300_90ppi_0.png" class="bg"/> <img src="https://opensource.org/files/osi_keyhole_300X300_90ppi_0.png" class="bg"/> <img src="https://opensource.org/files/osi_keyhole_300X300_90ppi_0.png" class="bg"/> <img src="https://opensource.org/files/osi_keyhole_300X300_90ppi_0.png" class="bg"/> <img src="https://opensource.org/files/osi_keyhole_300X300_90ppi_0.png" class="bg"/> <img src="https://opensource.org/files/osi_keyhole_300X300_90ppi_0.png" class="bg"/> <img src="https://opensource.org/files/osi_keyhole_300X300_90ppi_0.png" class="bg"/> </div> </div>

position:sticky can do it: position:sticky可以做到:

 .bg { height: 50px; width: 50px; display: flex; } .container { display: flex; overflow-x: auto; width: 300px; position: relative; flex-direction: row; } .overlay { position: sticky; width:inherit; margin-right:-300px; flex-shrink:0; left:0; background-color: #888888; opacity: 0.8; }
 <div class="container"> <div class="overlay"> </div> <img src="https://opensource.org/files/osi_keyhole_300X300_90ppi_0.png" class="bg" /> <img src="https://opensource.org/files/osi_keyhole_300X300_90ppi_0.png" class="bg" /> <img src="https://opensource.org/files/osi_keyhole_300X300_90ppi_0.png" class="bg" /> <img src="https://opensource.org/files/osi_keyhole_300X300_90ppi_0.png" class="bg" /> <img src="https://opensource.org/files/osi_keyhole_300X300_90ppi_0.png" class="bg" /> <img src="https://opensource.org/files/osi_keyhole_300X300_90ppi_0.png" class="bg" /> <img src="https://opensource.org/files/osi_keyhole_300X300_90ppi_0.png" class="bg" /> <img src="https://opensource.org/files/osi_keyhole_300X300_90ppi_0.png" class="bg" /> <img src="https://opensource.org/files/osi_keyhole_300X300_90ppi_0.png" class="bg" /> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM