简体   繁体   English

悬停时更改基础svg颜色

[英]Change foundation svg color on hover

Hello so I've seen a lot of codes regarding the hover color change on svg's but I can't get it to work. 您好,我在svg上看到了很多有关悬停颜色变化的代码,但我无法使其正常工作。 The thing is I am using foundation icons , and here is a sample code: 问题是我正在使用基础图标 ,这是示例代码:

<img src="svgs/fi-mail.svg">

Any idea on changing the color on hover? 有任何关于在悬停时更改颜色的想法吗?

You have to convert the SVG into a native <svg> tag and then you need to apply DOM traversal and CSS to change it. 您必须将SVG转换为本地<svg>标记,然后需要应用DOM遍历和CSS进行更改。 I have a quick snippet, that I modified from elsewhere, and you can use it to change the properties. 我有一个简短的代码段,可以在其他地方进行修改,您可以使用它来更改属性。

You need to make the SVG to be an inline SVG . 您需要使SVG成为嵌入式SVG You can make use of this script, by adding a class svg to the image: 您可以通过向图像添加svg类来使用此脚本:

 /* * Replace all SVG images with inline SVG */ jQuery('img.svg').each(function(){ var $img = jQuery(this); var imgID = $img.attr('id'); var imgClass = $img.attr('class'); var imgURL = $img.attr('src'); jQuery.get(imgURL, function(data) { // Get the SVG tag, ignore the rest var $svg = jQuery(data).find('svg'); // Add replaced image's ID to the new SVG if(typeof imgID !== 'undefined') { $svg = $svg.attr('id', imgID); } // Add replaced image's classes to the new SVG if(typeof imgClass !== 'undefined') { $svg = $svg.attr('class', imgClass+' replaced-svg'); } // Remove any invalid XML tags as per http://validator.w3.org $svg = $svg.removeAttr('xmlns:a'); // Replace image with new SVG $img.replaceWith($svg); }, 'xml'); }); 

And then, now if you do: 然后,如果您这样做:

 .logo-img path { fill: #000; } 

Or may be: 或者可能:

 .logo-img path { background-color: #000; } 

This works! 这可行!

Reference: img src SVG changing the fill color 参考: img src SVG更改填充颜色

You cannot change the color of an SVG that is included via an IMG tag. 您无法更改通过IMG标签包含的SVG的颜色。 You can only modify inline SVG through CSS. 您只能通过CSS修改内联SVG。

But the Zurb Foundation Icons are normally included as web-font, where each icon is just a simple text character, which can be styled as any other text. 但是Zurb Foundation图标通常包含在网络字体中,其中每个图标只是一个简单的文本字符,可以像其他任何文本一样设置样式。 The idea to include a single SVG icon via an IMG tag deviates from the Foundation documentation. 通过IMG标签包含单个SVG图标的想法与Foundation文档有所不同。 Please refer to the „How to use“ section in the docs-article you linked above… 请参考您上面链接的文档文章中的“如何使用”部分...

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

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