【问题标题】:Reset a css property on :hover在 :hover 上重置 CSS 属性
【发布时间】:2013-04-06 21:37:12
【问题描述】:

我正在使用 svg 将堆栈溢出的徽标嵌入到我的简历中。 svg xml 包含徽标的填充颜色。

这里是svg内容

<svg xmlns:svg="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg" version="1.1" width="100%" height="100%" viewBox="0 0 64 64" id="svg3715">
<defs id="defs3717"/>
<g id="layer1">
<path d="m 9.3049611,36.847632 4.4013079,0.04316 -0.153442,19.598393 29.291259,0 0,-19.527506 4.637782,0 0,24.287331 -38.2006795,0 0.023777,-24.401371 z" id="path2830" fill="#919191" style="fill-opacity:1;stroke:none"/>
<rect width="22.944817" height="4.881876" x="16.481951" y="48.434078" id="rect3604" fill="#919191" style="fill-opacity:1;stroke:none"/>
<rect width="23.066864" height="5.0039229" x="20.749949" y="37.830307" transform="matrix(0.9953749,0.09606666,-0.09606666,0.9953749,0,0)" id="rect3606" fill="#A78B68" style="fill-opacity:1;stroke:none"/>
<rect width="23.066864" height="5.0039229" x="26.472515" y="23.401554" transform="matrix(0.96240291,0.27162592,-0.27162592,0.96240291,0,0)" id="rect3606-1" fill="#c19653" style="fill-opacity:1;stroke:none"/>
<rect width="23.066864" height="5.0039229" x="30.528769" y="3.1535864" transform="matrix(0.85597805,0.51701216,-0.51701216,0.85597805,0,0)" id="rect3606-1-3" fill="#D48C28" style="fill-opacity:1;stroke:none"/>
<rect width="23.066864" height="5.0039229" x="27.191883" y="-24.475019" transform="matrix(0.58242689,0.81288309,-0.81288309,0.58242689,0,0)" id="rect3606-1-3-7" fill="#FE8908" style="fill-opacity:1;stroke:none"/>
<rect width="23.066864" height="5.0039229" x="11.174569" y="-50.183693" transform="matrix(0.16480989,0.98632535,-0.98632535,0.16480989,0,0)" id="rect3606-1-3-7-6" fill="#FF7A15" style="fill-opacity:1;stroke:none"/>
</g>
</svg>

我可以使用

将徽标设置为黑色(我希望它在正常状态下的样子)
rect, path
{
    fill: black;
}

但是现在,我该怎么做才能删除:hover 上的属性,以便让徽标恢复它的亮度?

我试图查看fill 参数documentation。但我没有发现任何帮助。

【问题讨论】:

  • 删除属性是什么意思?您可以将填充设置为无 :) 我也不明白关于“[...] 找回它的亮度”的部分
  • 试过rect:hover, path:hover { fill: none; } ?
  • 我想我知道 OP 的意思 - 他希望在悬停后返回徽标颜色。
  • @Terry 这正是我的意思。
  • @m.spyratos fill: none 删除所有颜色信息。

标签: html css svg hover


【解决方案1】:

这是可能的 - 只需在 CSS 中使用 :not() 选择器。但有些浏览器可能不支持它:

svg:not(:hover) rect,
svg:not(:hover) path {
    fill: black;
}

在这里查看小提琴 - http://jsfiddle.net/teddyrised/qvV8Q/

[编辑]:为了完整起见 - 如果您想为不支持 :not() 选择器的浏览器添加兼容性,您只需相应地切换 .hover 类和样式:

$(document).ready(function() {
    $("svg").hover(function() {
        $(this).attr("class", "hover");
    }, function() {
        $(this).attr("class", "");
    });
});

注意:之所以使用.attr(),是因为add/removeClass方法本身不适用于&lt;svg&gt;元素。但是,这可以通过使用这个插件来解决 - http://keith-wood.name/svg.html#dom

还有 CSS:

.hover rect, .hover path {
    fill: black;
}

可以在此处查看 JS 驱动的替代方案 - http://jsfiddle.net/teddyrised/LTRCB/

【讨论】:

  • 哇,这太棒了。非常感谢。
猜你喜欢
  • 1970-01-01
  • 2021-07-30
  • 2012-07-07
  • 2011-03-23
  • 2023-03-28
  • 1970-01-01
  • 2023-03-08
  • 2014-11-22
  • 2019-06-12
相关资源
最近更新 更多