在单击时更改SVG图像样式/效果,通过代码而不渲染新的SVG图像

时间:2018-04-24 04:48:55

标签: javascript css html5 angular svg

Image to be changed on click of image

Default image rendered on page load

<div id="svgDiv">
<img src="SVG_90_default.svg"/>
</div>

这里,在div中,我直接在img标签'SVG_90_default.svg'中渲染SVG图像。但是,单击此图像时,其颜色和文本应更改为白色,如附加图像中所示。我不想在点击时加载其他图像,因为它会影响性能。那么,我们如何通过代码在这里改变嵌入式SVG图像的颜色呢?

------- ----- SVG_90_default.svg

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 118.46 116"><defs><style>.cls-1,.cls-3{fill:none;stroke-miterlimit:10;}.cls-1{stroke:#6e7b9f;opacity:0.5;}.cls-2,.cls-4{fill:#36c;}.cls-3{stroke:#36c;stroke-linecap:round;stroke-width:2px;}.cls-4{font-size:14.42px;font-family:Calibri, Calibri;}</style></defs><title>90S_Default</title><g id="Layer_2" data-name="Layer 2"><g id="Collimator_Change_Default" data-name="Collimator Change Default"><g id="_90S_Default" data-name="90S_Default"><rect class="cls-1" x="1.73" y="0.5" width="115" height="115" rx="2" ry="2"/><rect class="cls-2" x="44.78" y="21" width="28.9" height="10.84" rx="2" ry="2"/><rect class="cls-2" x="22.02" y="40.78" width="29.77" height="11.16" rx="2" ry="2" transform="translate(-9.46 83.27) rotate(-90)"/><path class="cls-3" d="M39.13,65.71A27.9,27.9,0,1,0,78.83,26.5"/><text class="cls-4" transform="translate(51.92 101.07)">90</text></g></g></g></svg>

--------------- SVG_90_OnClick.svg -----------------------

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 116 116"><defs><style>.cls-1{fill:#36c;stroke:#36c;}.cls-1,.cls-3{stroke-miterlimit:10;}.cls-2,.cls-4{fill:#f2f2fc;}.cls-3{fill:none;stroke:#f2f2fc;stroke-linecap:round;stroke-width:2px;}.cls-4{font-size:14px;font-family:Calibri, Calibri;}</style></defs><title>90S_Selected</title><g id="Layer_2" data-name="Layer 2"><g id="Collimator_Change_Selected" data-name="Collimator Change_Selected"><g id="_90S_Selected" data-name="90S_Selected"><rect class="cls-1" x="0.5" y="0.5" width="115" height="115" rx="2" ry="2"/><rect class="cls-2" x="43.55" y="21" width="28.9" height="10.84" rx="2" ry="2"/><rect class="cls-2" x="21.87" y="42.67" width="28.9" height="10.84" rx="2" ry="2" transform="translate(-11.76 84.41) rotate(-90)"/><path class="cls-3" d="M38.49,66.87A27.09,27.09,0,1,0,77,28.8"/><text class="cls-4" transform="translate(50.9 101.2)">90</text></g></g></g></svg>

2 个答案:

答案 0 :(得分:0)

img标记无效。您需要在此处使用svg元素。

您可以使用:focus伪类在点击时更改svg的fillstroke属性。您需要使用tabindex:focus工作

&#13;
&#13;
* {
  outline: none;
}

svg:focus .cls-1 {
  fill: #3366cc;
}

svg:focus .cls-2,
svg:focus .cls-4 {
  fill: white;
}

svg:focus .cls-3 {
  stroke: white;
}
&#13;
<svg tabindex="1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 118.46 116" width="100" height="100"><defs><style>.cls-1,.cls-3{fill:none;stroke-miterlimit:10;}.cls-1{stroke:#6e7b9f;}.cls-2,.cls-4{fill:#36c;}.cls-3{stroke:#36c;stroke-linecap:round;stroke-width:2px;}.cls-4{font-size:14.42px;font-family:Calibri, Calibri;}</style></defs><title>90S_Default</title><g id="Layer_2" data-name="Layer 2"><g id="Collimator_Change_Default" data-name="Collimator Change Default"><g id="_90S_Default" data-name="90S_Default"><rect class="cls-1" x="1.73" y="0.5" width="115" height="115" rx="2" ry="2"/><rect class="cls-2" x="44.78" y="21" width="28.9" height="10.84" rx="2" ry="2"/><rect class="cls-2" x="22.02" y="40.78" width="29.77" height="11.16" rx="2" ry="2" transform="translate(-9.46 83.27) rotate(-90)"/><path class="cls-3" d="M39.13,65.71A27.9,27.9,0,1,0,78.83,26.5"/><text class="cls-4" transform="translate(51.92 101.07)">90</text></g></g></g></svg>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

如果是单纯色图像,你可以使用css过滤器(例如svg,也可以使用img标签):

&#13;
&#13;
#svgDiv svg,
#svgDiv img {
  width: 100px;
}

#svgDiv:hover {
  background: #36c;
}

#svgDiv:hover svg,
#svgDiv:hover img {
  filter: brightness(100);
}
&#13;
<div id="svgDiv">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 118.46 116"><defs><style>.cls-1,.cls-3{fill:none;stroke-miterlimit:10;}.cls-1{stroke:#6e7b9f;opacity:0.5;}.cls-2,.cls-4{fill:#36c;}.cls-3{stroke:#36c;stroke-linecap:round;stroke-width:2px;}.cls-4{font-size:14.42px;font-family:Calibri, Calibri;}</style></defs><title>90S_Default</title><g id="Layer_2" data-name="Layer 2"><g id="Collimator_Change_Default" data-name="Collimator Change Default"><g id="_90S_Default" data-name="90S_Default"><rect class="cls-1" x="1.73" y="0.5" width="115" height="115" rx="2" ry="2"/><rect class="cls-2" x="44.78" y="21" width="28.9" height="10.84" rx="2" ry="2"/><rect class="cls-2" x="22.02" y="40.78" width="29.77" height="11.16" rx="2" ry="2" transform="translate(-9.46 83.27) rotate(-90)"/><path class="cls-3" d="M39.13,65.71A27.9,27.9,0,1,0,78.83,26.5"/><text class="cls-4" transform="translate(51.92 101.07)">90</text></g></g></g></svg>
</div>
&#13;
&#13;
&#13;