我希望背景图片的不透明度从 100% 变为 0%。我可以选择使用另一种图像 Assets ,我使用图像编辑器使图像淡出不透明度,但我想使用尽可能少的 Assets 。这可以用 CSS 完成吗?我知道我可以制作多个 div,并在其中更改每个 div 的不透明度,但这需要很多 div 才能使其看起来不错。
这是我的代码目前在我不想使用的解决方案中的样子:
<div class="contentFadeAway" id="cfa1"></div>
<div class="contentFadeAway" id="cfa2"></div>
<div class="contentFadeAway" id="cfa3"></div>
<div class="contentFadeAway" id="cfa4"></div>
<div class="contentFadeAway" id="cfa5"></div>
<div class="contentFadeAway" id="cfa6"></div>
<div class="contentFadeAway" id="cfa7"></div>
<div class="contentFadeAway" id="cfa8"></div>
<div class="contentFadeAway" id="cfa9"></div>
<div class="contentFadeAway" id="cfa10"></div>
还有 CSS:
.contentFadeAway {
display: block;
position: fixed;
top: 160px;
padding: 0px;
width: 100%;
height: 5px;
background: url('/assets/shapeimage_3_int.png') fixed;
background-size:cover;
z-index: +1;
}
#cfa1 { top: 160px; opacity: 1; }
#cfa2 { top: 165px; opacity: .9; }
#cfa3 { top: 170px; opacity: .8; }
#cfa4 { top: 175px; opacity: .7; }
#cfa5 { top: 180px; opacity: .6; }
#cfa6 { top: 185px; opacity: .5; }
#cfa7 { top: 190px; opacity: .4; }
#cfa8 { top: 195px; opacity: .3; }
#cfa9 { top: 200px; opacity: .2; }
#cfa10 { top: 205px; opacity: .1; }
对于那些不明白这段代码在做什么的人,请看这里:http://jsfiddle.net/FVNY7/2/我有一个背景图像,我希望内容在向上滚动时逐渐消失,所以我将使用不透明度从 1 到 0 的相同图像来产生这种效果。如果背景是纯色,我可以只使用 rgba 渐变,但它是图像。
最佳答案
为了获得最多的跨浏览器支持,请在您的 div 中设置您的背景图片。然后在其上覆盖另一个具有半透明渐变背景的 div。
HTML:
<div class="content"></div>
<div class="FadeAway"></div>
CSS:
.content{ position:absolute; top:0px; left:0px; width:100%; height:100%; background:url('http://upload.wikimedia.org/wikipedia/commons/thumb/0/0c/GoldenGateBridge-001.jpg/400px-GoldenGateBridge-001.jpg') no-repeat; }
.FadeAway{
position: absolute; top:0px; left:0px; width:100%; height:100%;
background:transparent;
background: linear-gradient(top, rgba( 255, 255, 255, 255 ) 0%, rgba( 255, 255, 255, 1 ) 100% );
background: -moz-linear-gradient(top, rgba( 255, 255, 255, 0) 0%, rgba( 255, 255, 255, 1 ) 100% );
background: -ms-linear-gradient(top, rgba( 255, 255, 255, 0 ) 0%, rgba( 255, 255, 255, 1 ) 100% );
background: -o-linear-gradient( top, rgba( 255, 255, 255, 0 ) 0%, rgba( 255, 255, 255, 1 ) 100% );
background: -webkit-linear-gradient( top, rgba( 255, 255, 255, 0 ) 0%, rgba( 255, 255, 255, 1 ) 100% );
-ms-filter: progid:DXImageTransform.Microsoft.gradient(startColorstr=#550000FF, endColorstr=#550000FF);
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr=#00ffffff, endColorstr=#ffffffff);
}
这是上面例子的 fiddle :http://jsfiddle.net/FVNY7/
关于CSS - 图像上的透明度渐变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11594370/