28

我使用 svg 路径在我的项目中使用现代 ui 图标包。

我尝试做的是更改悬停时的填充颜色。

但我没有成功..

希望有人帮我解决这个问题。

提前致谢。

代码:

<div id="Main">
   <ul>
      <li>
          <form>
             <button>
                <div class="inner">
                   <svg>
                     <path d="M35.......etc..">
                     </path>
                   </svg>
                </div>
             </button>
          </form>
      </li>
   </ul>
</div
4

3 回答 3

43

您可以通过 css 覆盖 svg 填充颜色,如下所示,也可以针对不同的元素,如<path> polygon circle等。

#Main svg:hover {
    fill: #fce57e;
}

#Main svg:hover path {
    fill: #fce57e;
}

#Main svg:hover plygon {
    fill: #fce57e;
}

#Main svg:hover circle {
    fill: #fce57e;
}
于 2013-10-03T10:56:25.403 回答
15

很抱歉回答了一个非常古老的帖子。

我认为最好的方法是查看你的<svg>.

例如:

<svg>
    <path>...</path>
</svg>

如您所见,<path>标签由<svg>. 然后你可以<path>在css中以这种方式添加:

svg:hover path { 
    fill: #fce57e;
}

这是工作片段:

svg:hover path {
  fill: black;
  transition: all ease 0.3s;
}

svg path {
  transition: all ease 0.3s;
}
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 33.19 50.36" width="15%" class="go-location-all" id="go-location-icon"><defs><style>.cls-1{fill:#DF1E34;}</style></defs><title>find-location</title><g id="Layer_2" data-name="Layer 2"><g id="artwork"><path class="cls-1" d="M20.75,22.56a1.09,1.09,0,1,0-1-1.93,6.71,6.71,0,1,1,3.56-5.92,1.1,1.1,0,0,0,2.19,0,8.88,8.88,0,1,0-4.73,7.85Z"/><path class="cls-1" d="M15.88,44.28a1.07,1.07,0,0,0,.71.27,1.09,1.09,0,0,0,.67-.23,37.31,37.31,0,0,0,7-7.8,38.45,38.45,0,0,0,7-21.82,14.71,14.71,0,1,0-29.41,0c0,9,3.8,16.58,7,21.33A44.52,44.52,0,0,0,15.88,44.28Zm.71-42.09A12.53,12.53,0,0,1,29.11,14.71a36.16,36.16,0,0,1-6.57,20.5A38.39,38.39,0,0,1,16.63,42a46.29,46.29,0,0,1-6-7.22c-3-4.49-6.58-11.62-6.58-20.06A12.52,12.52,0,0,1,16.59,2.19Z"/><path class="cls-1" d="M24.51,40.69a1.1,1.1,0,0,0-.32,2.17C29.59,43.67,31,45,31,45.25s-.79,1-3.94,1.83a45.38,45.38,0,0,1-10.47,1.09A45.29,45.29,0,0,1,6.13,47.08C3,46.3,2.22,45.44,2.19,45.25S3.59,43.67,9,42.86a1.09,1.09,0,0,0-.32-2.16C4.7,41.29,0,42.57,0,45.25c0,1.66,1.8,3,5.36,3.9a46.84,46.84,0,0,0,11.23,1.21,47,47,0,0,0,11.24-1.21c3.55-.92,5.36-2.24,5.36-3.9C33.19,42.56,28.47,41.29,24.51,40.69Z"/></g></g></svg>

于 2018-02-04T18:24:00.580 回答
3

在某些情况下stroke使用而不是fill用于 svg,所以使用这个

svg:hover path { 
    stroke: #fce57e;
}

或者如果没有涉及路径:

svg:hover { 
    stroke: #fce57e;
}
于 2020-03-21T23:22:28.380 回答