here是一个编码:
Https://codepen.io/rradarr/full/mdwgard。 我希望红色矩形在“视差范围内”之外不可见。
* {
margin: 0;
}
html, body {
height: 100%
}
main {
position: relative;
width: 100%;
perspective: 1px;
transform-style: preserve-3d;
overflow-y: auto;
overflow-x: hidden;
height: 100vh;
background-color: blue;
}
.static {
min-height: 800px;
}
.parallax-container {
border: solid black 3px;
height: 600px;
width: 100%;
transform-style: preserve-3d;
position: relative;
}
.parallax-child {
position: relative;
width: 100%;
height: 100%;
transform: translateZ(-2px) scale(2.01);
z-index: -1;
}
#img-or-whatever {
height: 900px;
width: 100%;
background-color: red;
position: relative;
z-index: -1;
}
<main>
<div class="static"></div>
<div class="parallax-container">
<div class="parallax-child">
<div id="img-or-whatever"></div>
</div>
</div>
<div class="static"></div>
</main>
put something "above" the parallax element
如果真的没有选项将某些内容放在“上方”视差元素上方,您可以尝试使用JS类似的事情(例如thishis this,例如)。这不是理想的选择,因为它会暗示很多计算和变量,并且可能需要一些时间才能完全完成您想要的东西(并且由于您需要溢出,因此在容器内部放松了3D:无论如何都隐藏)。 如果您确实需要内部的3D,则可以使用JavaScript创建一个更复杂的解决方案,该解决方案也可以跳过。但是,如果不是强制性的,我会尽力避免这种情况(我宁愿在启用3D的
overflow: hidden
容器上添加一个绝对元素。如果您在该部分中仍然需要3D,则给出绝对的容器透明背景)。您可以尝试:不是()伪CLS,但是我不知道您会传递到类似于不在黑色正方形的括号中。通常,我还建议尝试避免使用这种东西(如果可能的话),但我认为您在这里没有很多选择。
contain可能是限制子元素超出其父母大小的侵入性的一种侵入性方法。例如,内联元素: