博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
如何使用CSS更改SVG图像的颜色(jQuery SVG图像替换)?
阅读量:2292 次
发布时间:2019-05-09

本文共 7729 字,大约阅读时间需要 25 分钟。

本文翻译自:

This is a self Q&A of a handy piece of code I came up with. 这是我想出的一小段代码的自我问答。

Currently, there isn't an easy way to embed an SVG image and then have access to the SVG elements via CSS. 当前,没有一种简单的方法可以嵌入SVG图像,然后可以通过CSS访问SVG元素。 There are various methods of using JS SVG frameworks, but they are overly complicated if all you are doing is making a simple icon with a rollover state. 有多种使用JS SVG框架的方法,但是如果您要做的只是制作带有过渡状态的简单图标,它们将过于复杂。

So here is what I came up with, which I think is by far the easiest way to use SVG files on a website. 所以这就是我想出的,我认为这是迄今为止在网站上使用SVG文件的最简单方法。 It takes its concept from the early text-to-image replacement methods, but as far as I am aware has never been done for SVGs. 它的概念来自早期的文本到图像替换方法,但据我所知,从未对SVG进行过处理。

This is the question: 这是问题:

How do I embed an SVG and change its color in CSS without using a JS-SVG framework? 如何在不使用JS-SVG框架的情况下在SVG中嵌入SVG并更改其颜色?


#1楼

参考:


#2楼

Firstly, use an IMG tag in your HTML to embed an SVG graphic. 首先,在HTML中使用IMG标签嵌入SVG图形。 I used Adobe Illustrator to make the graphic. 我使用Adobe Illustrator制作图形。

This is just like how you'd embed a normal image. 就像您嵌入普通图像一样。 Note that you need to set the IMG to have a class of svg. 请注意,您需要将IMG设置为具有svg类。 The 'social-link' class is just for examples sake. 'social-link'类仅出于示例目的。 The ID is not required, but is useful. 该ID不是必需的,但很有用。

Then use this jQuery code (in a separate file or inline in the HEAD). 然后使用此jQuery代码(在单独的文件中或HEAD中的内联中)。

/**     * 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');        });

What the above code does is look for all IMG's with the class 'svg' and replace it with the inline SVG from the linked file. 上面的代码所做的是查找所有带有“ svg”类的IMG,并将其替换为链接文件中的内联SVG。 The massive advantage is that it allows you to use CSS to change the color of the SVG now, like so: 巨大的优势在于,它允许您现在使用CSS来更改SVG的颜色,如下所示:

svg:hover path {    fill: red;}

The jQuery code I wrote also ports across the original images ID and classes. 我编写的jQuery代码还跨越了原始图像ID和类。 So this CSS works too: 因此,此CSS也适用:

#facebook-logo:hover path {    fill: red;}

Or: 要么:

.social-link:hover path {    fill: red;}

You can see an example of it working here: 您可以在此处查看其工作示例: :

We have a more complicated version that includes caching here: 我们有一个更复杂的版本,其中包括此处的缓存: :


#3楼

If you can include files (PHP include or include via your CMS of choice) in your page, you can add the SVG code and include it into your page. 如果您可以在页面中包含文件(PHP包含或通过选择的CMS包含),则可以添加SVG代码并将其包含到页面中。 This works the same as pasting the SVG source into the page, but makes the page markup cleaner. 这与将SVG源代码粘贴到页面中相同,但使页面标记更整洁。

The benefit is that you can target parts of your SVG via CSS for hover -- no javascript required. 好处是您可以通过CSS定位SVG的各个部分以进行悬停-不需要javascript。

You just have to use a CSS rule like this: 您只需要使用如下CSS规则:

#pathidorclass:hover { fill: #303 !important; }

Note that the !important bit is necessary to override the fill color. 请注意, !important位对于覆盖填充颜色是必需的。


#4楼

Alternatively you could use CSS mask , granted isn't good but you could use a fallback 另外,您可以使用CSS mask ,虽然授予不是很好,但是您可以使用后备

.frame {    background: blue;    -webkit-mask: url(image.svg) center / contain no-repeat;}

#5楼

@Drew Baker gave a great solution to solve the problem. @Drew Baker提供了一个很好的解决方案。 The code works properly. 该代码正常工作。 However, those who uses AngularJs may find lots of dependency on jQuery. 但是,使用AngularJs的人可能会发现很多对jQuery的依赖。 Consequently, I thought it is a good idea to paste for AngularJS users, a code following @Drew Baker's solution. 因此,我认为为AngularJS用户粘贴一个好主意,这是@Drew Baker解决方案之后的代码。

AngularJs way of the same code AngularJs的相同代码方式

1. Html : use the bellow tag in you html file: 1. HTML :在您的HTML文件中使用以下标签:

2. Directive : this will be the directive that you will need to recognise the tag: 2.指令 :这将是您需要识别标记的指令:

'use strict';angular.module('myApp')  .directive('svgImage', ['$http', function($http) {    return {      restrict: 'E',      link: function(scope, element) {        var imgURL = element.attr('src');        // if you want to use ng-include, then        // instead of the above line write the bellow:        // var imgURL = element.attr('ng-include');        var request = $http.get(          imgURL,          {'Content-Type': 'application/xml'}        );        scope.manipulateImgNode = function(data, elem){          var $svg = angular.element(data)[4];          var imgClass = elem.attr('class');          if(typeof(imgClass) !== 'undefined') {            var classes = imgClass.split(' ');            for(var i = 0; i < classes.length; ++i){              $svg.classList.add(classes[i]);            }          }          $svg.removeAttribute('xmlns:a');          return $svg;        };        request.success(function(data){          element.replaceWith(scope.manipulateImgNode(data, element));        });      }    };  }]);

3. CSS : 3. CSS

.any-class-you-wish{    border: 1px solid red;    height: 300px;    width:  120px}

4. Unit-test with karma-jasmine : 4.用业力茉莉花进行单元测试

'use strict';describe('Directive: svgImage', function() {  var $rootScope, $compile, element, scope, $httpBackend, apiUrl, data;  beforeEach(function() {    module('myApp');    inject(function($injector) {      $rootScope = $injector.get('$rootScope');      $compile = $injector.get('$compile');      $httpBackend = $injector.get('$httpBackend');      apiUrl = $injector.get('apiUrl');    });    scope = $rootScope.$new();    element = angular.element('
'); element = $compile(element)(scope); spyOn(scope, 'manipulateImgNode').andCallThrough(); $httpBackend.whenGET(apiUrl + 'me').respond(200, {}); data = '
' + '
' + '' + '
' + '
' + '
' + '
' + '
' + '
' + '
' + '
'; $httpBackend.expectGET('/icons/icon-man.svg').respond(200, data); }); afterEach(function() { $httpBackend.verifyNoOutstandingExpectation(); $httpBackend.verifyNoOutstandingRequest(); }); it('should call manipulateImgNode atleast once', function () { $httpBackend.flush(); expect(scope.manipulateImgNode.callCount).toBe(1); }); it('should return correct result', function () { $httpBackend.flush(); var result = scope.manipulateImgNode(data, element); expect(result).toBeDefined(); }); it('should define classes', function () { $httpBackend.flush(); var result = scope.manipulateImgNode(data, element); var classList = ["svg"]; expect(result.classList[0]).toBe(classList[0]); });});

#6楼

If we have a greater number of such svg images we can also take the help of font-files. 如果我们有更多这样的svg图像,我们也可以借助字体文件。

Sites like can get us a font file from our svgs. 网站可以从svgs中获取字体文件。


Eg 例如

@font-face {    font-family: 'iconFont';    src: url('iconFont.eot');}#target{    color: white;    font-size:96px;    font-family:iconFont;}

转载地址:http://ovcnb.baihongyu.com/

你可能感兴趣的文章
Linux发送邮件
查看>>
YUM安装PHP5.6
查看>>
YUM源安装MySQL5.7
查看>>
Tomcat日志切割cronolog
查看>>
glibc-2.14安装
查看>>
升级openssl zlib版本 安装nginx
查看>>
ab压力测试
查看>>
SVN指定端口启动
查看>>
网站访问速度一般检查参数
查看>>
编译安装过程
查看>>
HTTP常见返回码信息
查看>>
WEB集群session处理方案
查看>>
JDK命令行(jps、jstat、jinfo、jmap、jhat、jstack、jstatd、hprof)与JConsole
查看>>
JAVA 对象访问: 句柄和指针
查看>>
dubbo 报错:java.lang.NoClassDefFoundError: org/I0Itec/zkclient/exception/ZkNoNodeException
查看>>
logback的使用和logback.xml详解
查看>>
Linux 快捷键
查看>>
JPA 联合主键配置
查看>>
ObjectAlreadyExistsException:Unable to store Job : '*', because one already exists with thi s ident
查看>>
这些大厂面试真题你能答出来,年薪至少30-50W,想不想挑战一下?
查看>>