简体   繁体   English

如何叠加两个透明背景图像

[英]How to overlay two transparent background images

I need to overlay few background images, of two different divs, I am using transparent PNGs but when I overlay one over the other the second adds some annoying white background over the first and the effect is lost. 我需要覆盖两个不同div的几个背景图像,我使用的是透明PNG,但是当我一个覆盖另一个div时,第二个会在第一个上添加一些令人讨厌的白色背景,并且效果会丢失。 I have read somewhere that this might be due to the alpha channel, but did not have any info for the walkthrough. 我在某处读到这可能是由于alpha通道引起的,但没有有关演练的任何信息。 Could someone point me to a solution ? 有人可以指出我的解决方案吗?

Please Note I am using a sprite and take both images from there, jQuery is included as well (so I will accept JS or jQuery solution), I need to use a transparent images, cannot use opaque, cannot combine the images. 请注意,我正在使用一个精灵并从那里获取两个图像,jQuery也包括在内(因此我将接受JS或jQuery解决方案),我需要使用透明图像,不能使用不透明图像,不能合并图像。 I currently I am using z-index: 1; 我目前正在使用z-index: 1; for the bottom layer and z-index:2; 对于底层和z-index:2; for the top, probably will have a third one so the solution should not be centered over overlaying two images, but to overlay 2+ images. 对于顶部,可能会有第三个,因此解决方案不应以覆盖两个图像为中心,而是覆盖2个以上图像。

Thanks! 谢谢!

The Image I am using 我正在使用的图像

Added a fiddle with comment in the JS section 在JS部分中添加了带有注释小提琴

A screenshot of the result notice the white between the leafs in the left upper corner and it is supposed to be a 35px square containing a little dirt 结果的屏幕截图显示左上角叶子之间的白色,应该是一个35像素的正方形,其中包含一点污垢

Solution: 解:

By using CSS and adding background-color: transparent; 通过使用CSS并添加background-color: transparent; I have removed the background color of the divs and obtained full transperancy 我删除了div的背景色并获得了完全透明

A div is not necessarily transparent by default. 默认情况下, div不一定是透明的。 It inherits the background of it's parent. 它继承了其父级的背景。 Which is most likely white, and not transparent.. 这很可能是白色的,而不是透明的。

Try: 尝试:

<div style="background-color: rgba(255, 0, 0, 0.5)">.........</div>

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

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