CSS - 图像上的透明度渐变
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11594370/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CSS - Transparency Gradient on an Image
提问by michaellindahl
I would like my background image to go from 100% opacity to 0% opacity. I could choose to use another image asset where I use an image editor to make the image fade opacity, however I want to use as little assets as possible. Can this be done with CSS? I know I could make several divs in which I change the opacity on each one, however this would require a lot of divs to make it look good.
我希望我的背景图像从 100% 不透明度变为 0% 不透明度。我可以选择使用另一个图像资源,我使用图像编辑器使图像淡出不透明度,但是我想使用尽可能少的资源。这可以用 CSS 来完成吗?我知道我可以制作多个 div,在其中更改每个 div 的不透明度,但是这需要很多 div 才能使其看起来不错。
This is what my code currently looks like with the solution I don't want to use:
这是我的代码目前在我不想使用的解决方案中的样子:
<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>
And the CSS:
和 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; }
For those that don't understand what that code is doing it is here: http://jsfiddle.net/FVNY7/2/I have a background image, and I want the content to fade away when it scrolls up, so I would have the same image with an opacity from 1 to 0 to give that effect. If the background was a solid color I could just use a rgba gradient, but its an image.
对于那些不明白该代码在做什么的人来说,这里是:http: //jsfiddle.net/FVNY7/2/我有一个背景图片,我希望内容在向上滚动时消失,所以我会具有相同的图像,不透明度从 1 到 0 以产生这种效果。如果背景是纯色,我可以只使用 rgba 渐变,但它是图像。
采纳答案by michaellindahl
My solution to my problem is to simply state that this is not possible with the current technology. An alternative option would be to use a simple transparency gradient. Until A better solution arrises this is what I will end up doing.
我对我的问题的解决方案是简单地说,这在当前的技术中是不可能的。另一种选择是使用简单的透明度渐变。在出现更好的解决方案之前,这就是我最终要做的。
background: linear-gradient(to bottom, rgba(0,0,0,1) 0%,rgba(0,0,0,0) 100%);
回答by Chris
For the most cross-browser support, set your background image in your div. Then overlay another div with a semi-transparent gradient background on top of it.
对于大多数跨浏览器支持,请在 div 中设置背景图像。然后在另一个 div 上覆盖一个半透明的渐变背景。
HTML:
HTML:
<div class="content"></div>
<div class="FadeAway"></div>
CSS:
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);
}
Here's a fiddle of the above example: http://jsfiddle.net/FVNY7/
这是上面示例的小提琴:http: //jsfiddle.net/FVNY7/
回答by michaellindahl
Although it may not be the best implementation and there could be a better way the best way I have found is the down and dirty implementation that I mentioned in my question. Using PHP code it can be more refined and look good. Here is the code:
尽管它可能不是最好的实现,并且可能有更好的方法,但我发现的最佳方法是我在问题中提到的低级和肮脏的实现。使用 PHP 代码,它可以更加精致和美观。这是代码:
<style>
.contentFadeAway {
display: block;
position: fixed;
top: 160px;
padding: 0px;
width: 100%;
height: 1px;
background: url('/assets/shapeimage_3_int.png') fixed;
background-size:cover;
z-index: +1;
}
</style>
<?php
for ($int = "1"; $int <= "50"; $int++) {
echo "<div class=\"contentFadeAway\" style=\"top: " . (160 + 1 * $int) . "px; opacity: " . (1 - .02 * $int) . ";\"></div>\";
";
}
?>