[英]CSS Grayscale Filter + Colored Gradient
我正在尝试使用纯CSS创建一种效果,其中:
彩色背景图像是灰度的,并已应用了渐变。 和
悬停时,渐变淡出,颜色淡入。
在下面,我尝试了两种技术,每种似乎都可以解决一半的问题。 后退是制作图像的彩色和灰度版本,但是显然我想避免这样做,以最大程度地减少加载时间。 希望您有任何想法-我有些困惑。
谢谢!
技术1:渐变加上背景混合模式
在这里,我将渐变直接应用于背景图像,并且通过结合白色背景和背景混合模式属性来实现灰度效果。
这样会导致整体上图像更暗,但这很好-更大的问题是过渡似乎不起作用,因此图像会立即从一种模式跳到另一种模式,而不是通过缓慢的淡入淡出。
https://jsfiddle.net/Lparts4j/1/
HTML:
<div class="test"></div>
CSS:
.test {
width: 400px;
height: 400px;
background: linear-gradient(135deg, rgb(0, 47, 75) 0%, rgb(220, 66, 37) 80%), url("http://i.imgur.com/ulb7EVg.jpg");
background-color: white;
background-blend-mode: multiply, luminosity;
background-size: cover;
-webkit-transition: all .5s ease-in-out;
-moz-transition: all .5s ease-in-out;
-o-transition: all .5s ease-in-out;
transition: all .5s ease-in-out; }
.test:hover {
background: url("http://i.imgur.com/ulb7EVg.jpg");
background-color: white;
background-blend-mode: normal;
background-size: cover; }
技术2:元素之前的灰度滤镜加渐变
在这里,我使用:before元素应用了渐变,并且通过filter属性实现了灰度效果。
淡入淡出使用此方法。 但是,我无法将渐变与灰度组合在一起-灰度滤镜最终也将应用于:before元素,以使渐变失去所有颜色。
在jsfiddle中,左图像同时具有渐变和灰度滤镜,而右图像仅具有渐变。
https://jsfiddle.net/548afgjf/4/
HTML:
<div class="img img-one"></div>
<div class="img img-two"></div>
CSS:
.img {
position: relative;
display: inline-block;
width: 300px;
height: 400px; }
.img-one {
background: url('http://i.imgur.com/ulb7EVg.jpg') center center / cover no-repeat;
-webkit-filter: grayscale(100%);
filter: grayscale(100%);
-webkit-transition: all .5s ease-in-out;
-moz-transition: all .5s ease-in-out;
-o-transition: all .5s ease-in-out;
transition: all .5s ease-in-out;
}
.img-one:before {
content:'';
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
opacity: .6;
background: rgb(0, 47, 75);
background: -moz-linear-gradient(-45deg, rgb(0, 47, 75) 0%, rgb(220, 66, 37) 100%);
background: -webkit-gradient(linear, left top, right bottom, color-stop(0%, rgb(0, 47, 75)), color-stop(100%, rgb(220, 66, 37)));
background: -webkit-linear-gradient(-45deg, rgb(0, 47, 75) 0%, rgb(220, 66, 37) 100%);
background: -o-linear-gradient(-45deg, rgb(0, 47, 75) 0%, rgb(220, 66, 37) 100%);
background: -ms-linear-gradient(-45deg, rgb(0, 47, 75) 0%, rgb(220, 66, 37) 100%);
background: linear-gradient(135deg, rgb(0, 47, 75) 0%, rgb(220, 66, 37) 100%);
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#002f4b', endColorstr='#dc4225', GradientType=1);
-webkit-transition: opacity .6s ease-in-out;
-moz-transition: opacity .6s ease-in-out;
-o-transition: opacity .6s ease-in-out;
transition: opacity .6s ease-in-out;
}
.img-one:hover {
-webkit-filter: grayscale(0%);
filter: grayscale(0%);
}
.img-one:hover:before {
opacity: 0; }
.img-two {
background: url('http://i.imgur.com/ulb7EVg.jpg') center center / cover no-repeat;
-webkit-transition: all .5s ease-in-out;
-moz-transition: all .5s ease-in-out;
-o-transition: all .5s ease-in-out;
transition: all .5s ease-in-out;
}
.img-two:before {
content:'';
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
opacity: .6;
background: rgb(0, 47, 75);
background: -moz-linear-gradient(-45deg, rgb(0, 47, 75) 0%, rgb(220, 66, 37) 100%);
background: -webkit-gradient(linear, left top, right bottom, color-stop(0%, rgb(0, 47, 75)), color-stop(100%, rgb(220, 66, 37)));
background: -webkit-linear-gradient(-45deg, rgb(0, 47, 75) 0%, rgb(220, 66, 37) 100%);
background: -o-linear-gradient(-45deg, rgb(0, 47, 75) 0%, rgb(220, 66, 37) 100%);
background: -ms-linear-gradient(-45deg, rgb(0, 47, 75) 0%, rgb(220, 66, 37) 100%);
background: linear-gradient(135deg, rgb(0, 47, 75) 0%, rgb(220, 66, 37) 100%);
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#002f4b', endColorstr='#dc4225', GradientType=1);
-webkit-transition: opacity .6s ease-in-out;
-moz-transition: opacity .6s ease-in-out;
-o-transition: opacity .6s ease-in-out;
transition: opacity .6s ease-in-out;
}
.img-two:hover {
}
.img-two:hover:before {
opacity: 0; }
过去,我曾尝试过灰度效果,而使它在所有浏览器上都能正常工作的唯一方法是将图像包装在SVG元素中,并将SVG滤镜应用于该元素。 您可能需要做同样的事情才能起作用。
在您的页面上,您需要定义SVG滤镜(这只是灰度级...您需要研究如何对颜色进行着色...)
<svg>
<defs>
<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>
</defs>
</svg>
接下来,将图像包装在SVG元素中:
<svg class="filterThis" width="100px" height="100px">
<image class="svgImg" xlink:href="image.png" height="100px" width="100px" />
</svg>
并在CSS中应用过滤器:
svg.filterThis .svgImg {
filter: url(#greyscale);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.