首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >要在单击时更改SVG图像样式/效果,请通过代码而不呈现新的SVG图像

要在单击时更改SVG图像样式/效果,请通过代码而不呈现新的SVG图像
EN

Stack Overflow用户
提问于 2018-04-24 12:48:55
回答 2查看 102关注 0票数 0

代码语言:javascript
复制
<div id="svgDiv">
<img src="SVG_90_default.svg"/>
</div>

在这里,在div中,我直接在img标记' SVG _90_default.svg‘中呈现SVG图像。但是,在单击此图像时,其颜色和文本应更改为白色,如附件中的图像。我不想在点击时加载另一个图像,因为这会影响性能。那么,我们如何通过这里的代码改变嵌入的SVG图像的颜色呢?

-SVG_90_default.svg

代码语言:javascript
复制
<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

代码语言:javascript
复制
<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>

EN

回答 2

Stack Overflow用户

发布于 2018-04-24 13:00:44

使用img标记时不可能。在这里,您需要使用svg元素。

您可以使用SVG伪类在单击时更改:focusfillstroke属性。您将需要使用tabindex来执行:focus工作

代码语言:javascript
复制
* {
  outline: none;
}

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

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

svg:focus .cls-3 {
  stroke: white;
}
代码语言:javascript
复制
<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>

票数 0
EN

Stack Overflow用户

发布于 2018-04-25 21:56:10

在单一纯色图像的情况下,您可以使用css滤镜(例如,svg也将与img标签一起使用):

代码语言:javascript
复制
#svgDiv svg,
#svgDiv img {
  width: 100px;
}

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

#svgDiv:hover svg,
#svgDiv:hover img {
  filter: brightness(100);
}
代码语言:javascript
复制
<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>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49993644

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档