How to change color of SVG image using CSS (jQuery SVG image replacement)?

后端 未结 17 1949
死守一世寂寞
死守一世寂寞 2020-11-21 17:36

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 vi

17条回答
  •  爱一瞬间的悲伤
    2020-11-21 18:02

    @Drew Baker gave a great solution to solve the problem. The code works properly. However, those who uses AngularJs may find lots of dependency on jQuery. Consequently, I thought it is a good idea to paste for AngularJS users, a code following @Drew Baker's solution.

    AngularJs way of the same code

    1. Html: use the bellow tag in you html file:

    
    

    2. Directive: this will be the directive that you will need to recognise the tag:

    '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:

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

    4. Unit-test with karma-jasmine:

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

提交回复
热议问题