当我在代码中使用特定的 svg 时,所有 svg 的颜色都会变为红色

web*_*.ic 2 html css svg angularjs

我有一些 SVG 图标,我在我的网络应用程序中使用它们。它们都有特定的颜色。不管是什么颜色,所有 SVG 图标都有一种颜色,而不是一种,它有两种颜色(灰色和红色)。

我有一个角度组件可以在我的 Web 应用程序中设置我的图标。我的组件的指令如下所示:

import {constants} from '../../../index.constants';

export class MyIcon{
    public icon: string;
    public iconsPath: string = constants.PATH_TO_ICONS;
    public fileExtension: string = '.svg';

    static options: ng.IComponentOptions = {
        template: '<div ng-include="ctrl.iconsPath + ctrl.icon + ctrl.fileExtension"></div>',
        controllerAs: 'ctrl',
        controller: MyIcon,
        bindings: {
            icon: '@'
        }
    };
}
Run Code Online (Sandbox Code Playgroud)

我可以在我的代码中使用我的组件,并通过一个名为 icon 的绑定,我可以设置我想要使用的图标的名称:

<my-icon icon="pencil"></my-icon>
Run Code Online (Sandbox Code Playgroud)

在附件中,您可以找到带有两种颜色的图标。当我在我的组件中使用它时,所有其他图标的颜色都会变成红色并且它们的形状会发生变化。

当我删除有问题的图标时,所有其他图标都有其特定的颜色,并且看起来像是必须的。我不知道为什么会发生这种情况,也许是因为错误的图标有两种颜色?

我注意到,当它们改变颜色时,故障图标的红色与所有其他图标的红色相同。

示例:我的导航中带有特定颜色且代码中没有错误图标的图标:

在此处输入图片说明

错误示例:当我在代码中使用错误图标时,导航中的图标和网络应用程序中的所有其他图标看起来像这样(红色和丑陋的形状):

在此处输入图片说明

错误的 SVG 图标:

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 15.62 15.62"><defs><style>.cls-1,.cls-4{fill:none;stroke-miterlimit:10;}.cls-1{stroke:#e30613;stroke-width:2px;}.cls-2{fill:#fff;}.cls-3{fill:#8b8c8d;}.cls-4{stroke:#fff;}</style></defs><title>Asset 7</title><g id="Layer_2" data-name="Layer 2"><g id="Layer_1-2" data-name="Layer 1"><circle class="cls-1" cx="7.5" cy="8.12" r="6.5"/><polygon class="cls-2" points="1.9 10.19 11.38 0.71 14.91 4.24 5.43 13.72 0.72 14.9 1.9 10.19"/><path class="cls-3" d="M11.38,1.41l2.83,2.83-9,9-3.77.94.94-3.77,9-9m0-1.41L1.45,9.93,0,15.59l5.66-1.41,9.93-9.93L11.38,0Z"/><polygon class="cls-3" points="0.03 15.59 3.43 14.74 0.88 12.19 0.03 15.59"/><rect class="cls-3" x="1.51" y="4.09" width="14.04" height="6" transform="translate(-2.51 8.11) rotate(-45)"/><line class="cls-4" x1="9.96" y1="1.41" x2="14.21" y2="5.66"/><line class="cls-1" x1="2.9" y1="3.52" x2="12.1" y2="12.72"/></g></g></svg>
Run Code Online (Sandbox Code Playgroud)

其他SVG:

<svg id="iArrowIcon" data-name="Layer 1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 7.06 12.71"><defs><style>#iArrowIcon .cls-1{fill:none;stroke-miterlimit:10;fill-rule:evenodd;}</style></defs><title>arrow</title><path id="_Compound_Path_" data-name="&lt;Compound Path&gt;" class="cls-1" d="M4904.11,1058.78l6,6-6,6" transform="translate(-4903.76 -1058.42)"/></svg>
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

azs*_*s06 5

您的 svg 具有相同的类,导致问题的 svg 覆盖了其他 svg 中定义的 css 属性。

您可以做的是,将父类或 id 添加到有问题的图标,并使用它来将它的 css 与其他人分开。

像这样的东西。

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>SVG ICONS</title>
</head>
<body>
	
<svg id="iStopIcon" class="iStopIcon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 15.62 15.62"><defs><style>
#iStopIcon .cls-1,#iStopIcon .cls-4{fill:none;stroke-miterlimit:10}#iStopIcon .cls-1{stroke:#e30613;stroke-width:2px}.cls-2{fill:#fff}.cls-3{fill:#8b8c8d}.cls-4{stroke:#fff}	
	</style></defs><title>Asset 7</title><g id="Layer_2" data-name="Layer 2"><g id="Layer_1-2" data-name="Layer 1"><circle class="cls-1" cx="7.5" cy="8.12" r="6.5"/><polygon class="cls-2" points="1.9 10.19 11.38 0.71 14.91 4.24 5.43 13.72 0.72 14.9 1.9 10.19"/><path class="cls-3" d="M11.38,1.41l2.83,2.83-9,9-3.77.94.94-3.77,9-9m0-1.41L1.45,9.93,0,15.59l5.66-1.41,9.93-9.93L11.38,0Z"/><polygon class="cls-3" points="0.03 15.59 3.43 14.74 0.88 12.19 0.03 15.59"/><rect class="cls-3" x="1.51" y="4.09" width="14.04" height="6" transform="translate(-2.51 8.11) rotate(-45)"/><line class="cls-4" x1="9.96" y1="1.41" x2="14.21" y2="5.66"/><line class="cls-1" x1="2.9" y1="3.52" x2="12.1" y2="12.72"/></g></g></svg>
	
<svg id="iArrowIcon" data-name="Layer 1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 7.06 12.71"><defs><style>
	#iArrowIcon .cls-1, .cls-4{fill:#ccc;stroke-miterlimit:10;fill-rule:evenodd;}</style></defs><title>arrow</title><path id="_Compound_Path_" data-name="&lt;Compound Path&gt;" class="cls-1" d="M4904.11,1058.78l6,6-6,6" transform="translate(-4903.76 -1058.42)"/></svg>	
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

在这里,我使用的是 Id,如果您愿意,也可以使用 class。