饱和度的工作方式与不透明度相同,您不能将效果应用于父项,然后在子项中撤消它。您最好的选择可能是使灰度图像成为您部分的子图像,但不是图像的父图像:
<section>
<figure class="desaturate" style="background-image: url('bg-img-1.jpg');"></figure>
<div class="row">
<div class="col-md-12">
<img src="img-2.jpg" class="img-responsive saturate" alt="" />
</div>
</div>
</section>
您的另一个选择是继承背景::before
or ::after
伪元素并在那里应用过滤器:
.desaturate {
width: 100%;
height: 100%;
background-position: center center;
background-repeat: no-repeat;
background-size: cover;
position: relative;
}
.desaturate:before {
content: '';
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
background: inherit;
/*grayscale for background image*/
-webkit-filter: grayscale(1);
-webkit-filter: grayscale(100%);
-moz-filter: grayscale(100%);
filter: gray;
filter: grayscale(100%);
filter: url("data:image/svg+xml;utf8,<svg version='1.1' xmlns='http://www.w3.org/2000/svg' height='0'><filter id='greyscale'><feColorMatrix type='matrix' values='0.3333 0.3333 0.3333 0 0 0.3333 0.3333 0.3333 0 0 0.3333 0.3333 0.3333 0 0 0 0 0 1 0' /></filter></svg>#greyscale");
}
您可以在这里查看演示:http://codepen.io/Godwin/pen/ogLPvR http://codepen.io/Godwin/pen/ogLPvR.
Edit:
只是为了感兴趣,还有另一种方法可以做到这一点,但它还没有很高的浏览器支持:
.desaturate {
width: 100%;
height: 100%;
background-position: center center;
background-repeat: no-repeat;
background-size: cover;
/*grayscale for background image*/
background-color: #FFF;
background-blend-mode: luminosity, normal;
}
参考:https://developer.mozilla.org/en-US/docs/Web/CSS/background-blend-mode https://developer.mozilla.org/en-US/docs/Web/CSS/background-blend-mode
例子:http://codepen.io/Godwin/pen/raLZVr http://codepen.io/Godwin/pen/raLZVr
支持:http://caniuse.com/#feat=css-backgroundblendmode http://caniuse.com/#feat=css-backgroundblendmode