简体   繁体   English

淡入div的背景图像(而不是主体或淡出背景颜色)

[英]Fade in a div's background-image (not the body or fading out a background-color)

Very simply, I'm trying to fade in the background-image of a div; 很简单,我试图淡入div的背景图像; I've seen this done with two divs and effectively revealing (ie: faux fading in) the background-image by fading out the background-color of a div covering the background-image. 我已经看到这是通过两个div完成的,并且通过淡出覆盖了背景图像的div的背景颜色来有效地揭示 (即,人造淡入)背景图像。 This will not solve my problem. 这不会解决我的问题。

The above mentioned solution works if the "mask" is the same solid color as the body background-color. 如果“遮罩”与主体背景色具有相同的纯色,则上述解决方案有效。 In my case, the body's background is a textured image, so this will not work. 在我的情况下,身体的背景是带纹理的图像,因此这是行不通的。 I could set the "mask" div to have the same background-image as the body, but having a z-indexed div seems like overkill. 我可以将“ mask” div设置为与主体具有相同的背景图像,但是使用z索引的div似乎有点过分。

tl;dr I have a div somewhere in the body--how do I fade in its background-image? tl; dr我的身体某处有一个div-如何淡化其背景图像? Thanks! 谢谢!

You need two divs. 您需要两个div。 The first one has no background color and the second one has a background image. 第一个没有背景颜色,第二个没有背景图像。 Fade the second one (Asherlc proposed a way) and you're done. 淡入第二个(Asherlc提出了一种方法),您就完成了。

.body {background-image:url(bg.png)}
.toFade {opacity=1 ... alternatives }
...

<div>
    <div class=toFade style="background-image:url(img.png)">
        <!-- possible content -->
    </div>
</div>

Not possible as far as I know. 据我所知不可能。 You will need 2 images / elements to do this. 您将需要2个图像/元素才能执行此操作。 You could use jquery / jquery ui's toggleClass with a duration. 您可以使用带有持续时间的jquery / jquery ui的toggleClass。

See examples here: 在此处查看示例:

How to add a fadeIn effect while changing background image using .css in Jquery 如何在jQuery中使用.css更改背景图像时添加fadeIn效果

jQuery li with image fade in background (fade in class) 具有图像淡入淡出的jQuery Li(在类中淡入淡出)

Ok! 好! It took me some time to solve this kind of "Challenge" and I think I came up with an idea close to your request! 我花了一些时间解决这种“挑战”,我想出了一个接近您要求的想法! Despite the fact that you want to FadeIn only your background-image with only one div I made a jQuery script using clone() method. 尽管事实上您只想在一个div中仅淡入背景图像 ,但我还是使用clone()方法制作了一个jQuery脚本。

The only thing that has a little work to do is if you want to fade In some text as well! 唯一要做的事就是是否也要淡入一些文字! Take a look of my script and let me know if it helped! 看看我的脚本,让我知道它是否有帮助!

http://jsfiddle.net/7z8vB/ http://jsfiddle.net/7z8vB/

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

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