gpt4 book ai didi

html - 固定图像背景上的 CSS 可变高度切口

转载 作者:行者123 更新时间:2023-11-27 23:28:01 25 4
gpt4 key购买 nike

以下代码模拟通过不透明背景的抠图,在下面的固定图像上滚动。切口的高度是动态的,大小适合其内容。

问题:代码使用了一个可怕的 hack!请注意,剪切图的内容 Y 在 HTML 中重复,但在视觉输出中只显示一次。

事情是——黑客“有效”。两个副本都是必需的:

  • 如果第一个被移除,剩余的内容 Y 呈现在图像后面,不再可见。
  • 如果第二个被移除,切口折叠到高度为零,剩余内容 Y 与内容 Z 重叠。

问题:是否有更好的纯 CSS 替代方案?

<!DOCTYPE html>
<html>
<head>
<title>Simulated Backdrop Image Cutout through Opaque Background</title>
<style>
.opaque-background {
background-color: black;
color: white;
}

.container {
position: relative;
}

.cutout {
background-attachment: fixed;
background-image: url(/image/RTFBR.jpg);
background-repeat: repeat;
position: absolute;
width: 100%;
height: 100%;
}
</style>
</head>
<body class="opaque-background">
Content preceding (X)
<div class="container">
<div class="cutout">
Content between (Y)
</div>
Content between (Y)
</div>
Content following (Z)
</body>
</html>

最佳答案

天哪,我觉得很害羞。答案就像调整 z-index 一样简单,以便剪切呈现在内容后面 Y。 (我以为我已经尝试过了,但显然没有。)

.cutout {
background-attachment: fixed;
background-image: url(/image/RTFBR.jpg);
background-repeat: repeat;
position: absolute;
width: 100%;
height: 100%;
z-index: -1;
}

HTML:

Content preceding (X)
<div class="container">
<div class="cutout">
</div>
Content between (Y)
</div>
Content following (Z)

关于html - 固定图像背景上的 CSS 可变高度切口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57646457/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com