您可以单独使用定位和 box-shadow 来完成,但浏览器支持会很差。我用过position: sticky
(不支持 Chrome),但无论如何都是一个有趣的实验。
<div class="wrap">
<!-- We set the overflow on the parent -->
<div class="shadow"></div>
<!-- Create a new container as a layer over the rest of the content -->
<div class="content">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
</div>
使用position: sticky
而不是absolute,.shadow
只要父级可见,就会一直停留在其父级的顶部,因此我们将其设置为父级的全高,并以负边距偏移内容以使其与父级的顶部对齐. Sticky 不会像绝对定位元素那样随着内容向下滚动。
您现在可以将 inset box-shadow 设置为任何值,并用于pointer-events: none
允许与具有 box-shadow 的层后面的层进行交互(因为具有较高 z-index 的定位元素会阻止您与它们后面的元素交互)。
.wrap{
border: 1px solid #dadada;
/* You'll need a fixed height - assuming that's the case already, given the overflow */
height: 400px;
margin: 5vh auto;
overflow-y: auto;
width: 50vw;
}
.content{
margin-top: -400px;
}
.shadow{
box-shadow: 10px -10px 10px rgba(0, 0, 0, 0.3) inset;
height: 100%;
/* To avoid objects below not being accessible we use pointer events - CSS4 and wonky IE support again */
pointer-events: none;
position: sticky;
/* Firefox doesn't really need the vendor prefix and Chrome doesn't support this */
position: -webkit-sticky;
position: -moz-sticky;
position: -ms-sticky;
position: -o-sticky;
top: 0;
width: 100%;
}
.item{
/* You would obviously add your own styling - just making boxes that show the concept */
background: white;
border: 1px solid #dadada;
box-sizing: border-box;
float: left;
height: 250px;
margin: 1%;
width: 23%;
}
.item:hover{
/* To demonstrate the click through the top layer and :hover works */
background: #f3f3f3;
}
再一次 - 这是实验性的,并且在某些地方缺乏浏览器支持,但证明你可以只使用 CSS 来做到这一点。
在这里摆弄...