如何在悬停事件中更改 SVG 图像中的笔触颜色? [英] How to Change Stroke Color in SVG Image on Hover Event?

查看:46
本文介绍了如何在悬停事件中更改 SVG 图像中的笔触颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在网站上搜索答案,但似乎没有人真正收到针对这个特定问题的明确答案.

I've been searching through the site for an answer, and it seems no one has really received a definitive answer for this specific question.

我希望更改鼠标悬停在我制作的 SVG 图标上的笔触颜色.

I'm looking to change the color of the stroke on an SVG icon that I made when it is hovered over.

这是我当前的代码:

<object data="price.svg" type="image/svg+xml" class="icon">
    <a href="price.svg" />
</object>

CSS:

.icon {width:100%}

推荐答案

您不能以这种方式更改外来对象的属性.但是,如果您可以将 SVG embed 嵌入到 HTML 文件中,那就很容易了.然后您可以引用 SVG 的 ID 并更改子元素的样式.

You can't change the properties of a foreign object that way. But it's easy if you can embed the SVG in the HTML file. Then you could reference the IDs of your SVG and change the style of the child elements.

替换

<object data="price.svg" ...> ... </object>

使用 SVG 文件的内容:

with the contents of your SVG file:

<svg ...><path id="styled-element" ...></svg>

现在您可以对其应用样式:

Now you can apply a style to it:

#styled-element:hover { stroke:red }

这篇关于如何在悬停事件中更改 SVG 图像中的笔触颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆