简体   繁体   English

如何在 Hover 上更改 svg 颜色

[英]How to change svg color on Hover

every one, i am using angular and i have a svg, html like:每一个,我都在使用 angular 并且我有一个 svg、html,例如:

  <button
    type="button"
    class="filter-icon-wrapper padding-0 border-0 outline-0 bg-transparent pointer"
  >
    <mat-icon class="filter-icon" *ngIf="!isDateType" svgIcon="filter_default">filter_alt</mat-icon>
  </button>

this is the svg source code filter_default:这是 svg 源代码 filter_default:

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 11.9 11.91"><defs><style>.cls-2{fill:#707070;}</style></defs><g id="Layer_2" data-name="Layer 2"><g id="Layer_1-2" data-name="Layer 1"><g id="Path_7587" data-name="Path 7587"><path class="cls-2" d="M6.8,11.91a1,1,0,0,1-.58-.19l-1.7-1.19a1,1,0,0,1-.43-.83V5.52L.3,1.73A1,1,0,0,1,0,1,1,1,0,0,1,.29.3,1,1,0,0,1,1,0h9.88a1,1,0,0,1,.72.29,1,1,0,0,1,0,1.44L7.82,5.52v5.37a1,1,0,0,1-.19.59A1,1,0,0,1,6.8,11.91ZM1,1,5.09,5.11V9.7L6.8,10.9l0-5.79L10.9,1V1Z"/></g></g></g></svg>

I want to know how can i change the svg color when mouse hover, thanks all of your help!我想知道如何更改鼠标hover时的svg颜色,谢谢大家的帮助!

 #Path_7587.cls-2 { fill:#707070; } #Path_7587.cls-2:hover { fill: red; }
 <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 11.9 11.91"><g id="Layer_2" data-name="Layer 2"><g id="Layer_1-2" data-name="Layer 1"><g id="Path_7587" data-name="Path 7587"><path class="cls-2" d="M6.8,11.91a1,1,0,0,1-.58-.19l-1.7-1.19a1,1,0,0,1-.43-.83V5.52L.3,1.73A1,1,0,0,1,0,1,1,1,0,0,1,.29.3,1,1,0,0,1,1,0h9.88a1,1,0,0,1,.72.29,1,1,0,0,1,0,1.44L7.82,5.52v5.37a1,1,0,0,1-.19.59A1,1,0,0,1,6.8,11.91ZM1,1,5.09,5.11V9.7L6.8,10.9l0-5.79L10.9,1V1Z"/></g></g></g></svg>

Similar to the first answer, with one correction.类似于第一个答案,有一个更正。 The right class to modify is cls-2 :正确的 class 修改为cls-2

.cls-2:hover {
  fill:red;
}

thanks all of your help, i try ng-deep and now work fine感谢您的所有帮助,我尝试了 ng-deep,现在工作正常

  1. rename the class name in svg from cls-2 to floatTextIcon将 svg 中的 class 名称从 cls-2 重命名为 floatTextIcon

  2. in the scss file add such code在 scss 文件中添加这样的代码

    :host::ng-deep.floatTextIcon:hover { fill: #3a464d; :host::ng-deep.floatTextIcon:hover { 填充:#3a464d; } }

Changing the icon color when hovering a button:悬停按钮时更改图标颜色:

button .cls-2 {
  fill: #888;
}

button:hover .cls-2 {
  fill: blue;
}

 button { width: 50px; height: 50px; } button.cls-2 { fill: #888; } button:hover.cls-2 { fill: blue; }
 <button> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 11.9 11.91"><g id="Layer_2" data-name="Layer 2" width="32" height="32"><g id="Layer_1-2" data-name="Layer 1"><g id="Path_7587" data-name="Path 7587"><path class="cls-2" d="M6.8,11.91a1,1,0,0,1-.58-.19l-1.7-1.19a1,1,0,0,1-.43-.83V5.52L.3,1.73A1,1,0,0,1,0,1,1,1,0,0,1,.29.3,1,1,0,0,1,1,0h9.88a1,1,0,0,1,.72.29,1,1,0,0,1,0,1.44L7.82,5.52v5.37a1,1,0,0,1-.19.59A1,1,0,0,1,6.8,11.91ZM1,1,5.09,5.11V9.7L6.8,10.9l0-5.79L10.9,1V1Z"/></g></g></g></svg> </button>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM