我想在网页中使用SVG滤镜feColorMatrix
。以前,我成功地使它同时适用于Firefox和Chrome(使用css属性filter:url()
)。但是,这在IE中不起作用,这需要我同时插入图像和SVG过滤器。
我正在寻找图像上的过滤器,当图像悬停时,该过滤器会淡出或消失。我想使用同一图像两次(一个带过滤器,另一个没有),所以我可以让过滤后的图像在悬停时消失,但未过滤的图像仍然可见。
问题是过滤器不再显示。两个图像均未过滤。我的问题是:到目前为止我的代码有什么问题?对于我想要达到的效果,有更好的解决方案吗?
提前感谢您的建议/指点!
这是我到目前为止的代码:
.css:
svg {
width:30%;
float:left;
overflow:hidden;
height:300px;
}
svg image {
position:absolute;
left:0px;
}
svg .img1 {
z-index:2;
}
svg .img2 {
z-index:1;
}
svg:hover .img1 {
opacity:0;
}
.html:
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" preserveAspectRatio="xMidYMax slice" viewBox="0 0 662 1000">
<defs>
<filter id="sepiatone">
<feColorMatrix values="0.3 0.4 0.2 0 0, 0.15 0.25 0.1 0 0, 0.05 0.2 0.1 0 0, 0 0 0 1 0" />
</filter>
</defs>
<image xlink:href="/CMS/uploads/img/upload_2.jpg" class='img1' filter="url(#sepiatone)" width='662px' height='1000px' />
<image xlink:href="/CMS/uploads/img/upload_2.jpg" class='img2' width='662px' height='1000px' />
</svg>
更新
我想我快到了!我没有在 SVG 中放置两个 image
标签,而是使用 use
标签对它进行了两次引用。此外,Michael指出z-index不起作用。因此,我将过滤后的图像放在 DOM 中的最新位置。将鼠标悬停在 SVG 上时,过滤后的 SVG 将改变不透明度,显示未过滤的图像。谢谢迈克尔的指点!我会接受你的回答,因为它指出了这个问题。
这是代码:
.HTML:
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" preserveAspectRatio="xMidYMax slice" viewBox="0 0 662 1000">
<defs>
<filter id="sepiatone">
<feColorMatrix values="0.3 0.4 0.2 0 0, 0.15 0.25 0.1 0 0, 0.05 0.2 0.1 0 0, 0 0 0 1 0"></feColorMatrix>
</filter>
<g id="imgR">
<image xlink:href="/CMS/uploads/img/upload_2.jpg" width='662px' height='1000px'></image>
</g>
</defs>
<use xlink:href="#imgR" class='img2'></use>
<use xlink:href="#imgR" class='img1' filter="url(#sepiatone)"></use>
</svg>
.CSS:
svg {
width:30%;
float:left;
overflow:hidden;
height:300px;
}
svg .img1, svg .img2 {
position:absolute;
left:0px;
width:100%;
}
svg:hover .img1 {
opacity:0;
}
SVG 不支持 z 索引。您想要在顶部的任何内容都必须在 DOM 中列在最后。