gpt4 book ai didi

javascript - 如何将 img 元素放在透明 img 元素后面?

转载 作者:行者123 更新时间:2023-11-28 17:29:13 25 4
gpt4 key购买 nike

我有两个 img 元素,我希望第一个 image.png 位于透明 image.png 的后面。我尝试了很多不同的东西(z-index、透明背景颜色、rgba 背景颜色、绝对定位和相对定位、在 div 中嵌套一个、使它们都成为 div)。现在我一直在尝试使用透明的 .png 图像。图像 .png 实际上在它后面,但它仍然通过它显示出来。请帮忙。

HTML:

<body>
<main class="site-wrapper">
<div class="carnival"></div>
<div id="images">
<img id="divbox" src="images/divbox.png">
<img id="clown1" src="images/clown1.png">
</div>
</main>
</body>

js:(我在 js b/c 中做了样式,我有兴趣学习如何以这种方式做):

//styles

//divbox:
document.getElementById('divbox').style.display = "inline-block";
document.getElementById('divbox').style.transform = "skew(-2deg)";
document.getElementById('divbox').style.marginTop = "21%";
document.getElementById('divbox').style.marginLeft = "47.6%";
document.getElementById('divbox').style.height = "200px";
document.getElementById('divbox').style.width = "200px";
document.getElementById('divbox').style.border = "1px solid orange";
document.getElementById('divbox').style.position = "absolute";
document.getElementById('divbox').style.zIndex = "2";

//clown1:
document.getElementById('clown1').style.display = "inline-block";
document.getElementById('clown1').style.transform = "rotate(90deg)";
document.getElementById('clown1').style.marginTop = "21%";
document.getElementById('clown1').style.marginLeft = "53%";
document.getElementById('clown1').style.border = "1px solid green";
document.getElementById('clown1').style.position = "relative";
document.getElementById('clown1').style.zIndex = "1";

感谢您的帮助,如果我可以回答问题,请告诉我。

更新:

抱歉没说清楚。我现在已经实现了将图像放在另一幅图像后面,但是由于顶部图像是透明的,后面的图像正在显示。我该如何阻止呢?

这是一个正在发生的事情的例子:

http://oi61.tinypic.com/2mw9egx.jpg

请注意橙色边框位于顶部,因此它绝对位于顶部。

更新 2:

这应该很清楚我想要什么。再次对造成的困惑表示抱歉:

http://oi59.tinypic.com/eamb0n.jpg

最佳答案

我会做类似下面的 jsFiddle 的事情:http://jsfiddle.net/7gdx48fu/ .可能需要重新加载几次才能看到覆盖工作的一个很好的例子。

为您的两个图像创建一个包装器 DIV。将该包装器 DIV 设置为 position: relative,这样我们就可以在它包含的其中一张图像上使用绝对定位。通过这样做,我们可以防止绝对定位的图像可能在页面的其他地方自行对齐,例如浏览器窗口的左上角。

然后,将我们的叠加图像(透明 PNG)的位置设置为 position: absolute 以及 top: 0left: 0 将其与第一张图片的左上角对齐。

如果您观看包含图像的顺序,则无需使用 z-index 即可执行此操作。首先将您想要的图像放在标记中透明 PNG 的后面,然后是透明 PNG。

<div class="img-container">
<img src="http://lorempixel.com/200/200/city/">
<img class="overlay" src="http://lorempixel.com/200/200/city">
</div>
.img-container {
position: relative;
}
.overlay {
position: absolute;
top: 0;
left: 0;
opacity: 0.25; /* using this to replicate the transparent PNG */
}

编辑

OP 的要求已更改为包括如何防止透明图像后面的图像透过透明图像显示。

这是一个更新的 jsFiddle:http://jsfiddle.net/7gdx48fu/2/ .

在这种方法中,我将透明的 PNG 包装在一个包装器 DIV 中,并设置它的背景颜色。我在示例中使用了白色,但您可以使用任何颜色。

<div class="img-container">
<img src="http://lorempixel.com/200/200/city/">
<div class="overlay">
<img src="http://lorempixel.com/200/200/city">
</div>
</div>
.img-container {
position: relative;
}
.overlay {
position: absolute;
top: 0;
left: 0;
background-color: white;
top: 15px; /* shifting overlay for illustrative purposes - not use case code */
left: 15px; /* shifting overlay for illustrative purposes - not use case code */
}
.overlay img {
opacity: 0.25; /* using this to replicate the transparent PNG */
}

不完美,但我不确定还能如何进行。

编辑 2

看来 OP 想要做一种掩蔽形式。您可以使用 overflow: hidden 来做到这一点。

我已经更新了 jsFiddle:http://jsfiddle.net/7gdx48fu/4/

在这个更新的答案中,我保留了包装器 DIV 并将其设置为固定的宽度和高度。然后应用 overflow: hidden。我们在这里所做的是创建一个不可见的窗口,该窗口仅在窗口尺寸范围内显示内容。

要使图像看起来像是来自基础层图像,只需调整包装 DIV 中图像的位置即可。对于 jsFiddle,只需使用 .mask imgtop 的值即可。

这需要对 .mask DIV 的适当位置和大小进行一些调整以满足您的需求,但希望能为您指明正确的方向。

<div class="img-container">
<img src="http://lorempixel.com/200/200/city/">
<div class="mask">
<img src="http://lorempixel.com/200/50/city">
</div>
</div>
.img-container {
position: relative;
}
.mask {
position: absolute;
top: 25px;
left: 25px;
height: 100px;
width: 100px;
overflow: hidden;
border: 1px solid red; /* for illustrative purposes */
}
.mask img {
position: relative;
top: 25px;
}

关于javascript - 如何将 img 元素放在透明 img 元素后面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26490003/

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