首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Chrome中的SVG 'use‘标签损坏

Chrome中的SVG 'use‘标签损坏
EN

Stack Overflow用户
提问于 2016-03-16 20:51:40
回答 2查看 3.7K关注 0票数 11

svg-sprite提供基于图标的导航功能(AngularJS和Angular Route)。所以,我有一个像这样的内联代码:

代码语言:javascript
复制
<div style="height: 0; width: 0; position: absolute; visibility: hidden">
<svg xmlns="http://www.w3.org/2000/svg">
    <symbol id="icon-grid-32" viewBox="0 0 32 32">
        <g stroke-width="2" stroke-linecap="round" stroke-miterlimit="10" stroke-linejoin="round">
            <path d="M2 2h11v11H2zM19 2h11v11H19zM2 19h11v11H2zM19 19h11v11H19z"/>
        </g>
    </symbol>
</svg>
</div>

导航中的图标如下所示:

代码语言:javascript
复制
<a href=""><svg class="icon icon-32 outline black"><use xlink:href="#icon-grid-32"></use></svg></a>

我在这个导航中看到的一切都是空的,<use>的大小是0×0像素。我知道火狐bug with xml:base,但是添加xml:base对我没有帮助。你可以试试这个例子:http://css.yoksel.ru/assets/demo/svg-in-firefox/svg-has-base.html

它可以在火狐、Safari和其他浏览器中运行,但不能在Chrome 49+中运行(48版本没有这个问题)。

EN

回答 2

Stack Overflow用户

发布于 2016-08-18 08:42:18

这是由于AngularJS对<base href="/" />和UI路由的依赖造成的,当应用程序不处于其“根”状态时,<use>元素中的相对散列链接将无法正确解析。

要解决此问题,您需要将xlink:href解析为使用绝对路径。您可以执行以下操作:

angular-icon-template.html

代码语言:javascript
复制
<svg class="c-icon" viewBox="0 0 32 32">
    <use xlink:href="" ng-attr-xlink:href="{{baseUrl + '#' + iconName}}" />
</svg>

angular-icon.js

代码语言:javascript
复制
angular.module('angularIcon', [])
    .directive('angularIcon', {
        templateUrl: 'angular-icon-template.html',
        scope: { iconName: '@' },
        controller: function($scope) {
            $scope.baseUrl = window.location.href.replace(window.location.hash, '');
        }
    });
票数 5
EN

Stack Overflow用户

发布于 2016-03-17 23:38:47

我遇到了与你所描述的问题非常相似的问题,不同的是我会在指令中生成我的图标<svg><use>

今天我一直在寻找一个更好的答案,并想出了一个解决<use>xlink:href问题的方法。它只是通过内联所需的svg来重新创建功能。

为了简单起见,我们假设我有一个通过属性icon-name接收图标名称的<angular-icon>指令

<angular-icon icon-name="{{someObject.iconName}}">

working指令现在如下所示:

代码语言:javascript
复制
angular.module('angularIcon', [])
.directive('angularIcon', IconDirective);

function IconDirective(){
    return{
        template:'',
        templateNamespace:'svg',

        link:function(scope, element, attributes){

            // my icon name comes from $http call so it doesnt exist when initialising the directive, 
            attributes.$observe( 'iconName', function(iconName){

                // let's grab the icon from the sprite
                var icon = angular.element( document.getElementById( iconName ) ); 
                // let's clone it so we can modify it if we want
                var iconClone = icon.clone();

                var namespace = "http://www.w3.org/2000/svg";

                // manually create the svg element and append the inlined icon as no other way worked
                var svg = document.createElementNS( namespace, 'svg' );
                svg.setAttribute( 'viewBox', '0 0 32 32' );
                svg.setAttribute( 'xml:space', 'preserve' );

                svg.appendChild( iconClone[0] );
                // let's add the newly created svg+icon to the directive's element
                element[0].appendChild( svg );

            });

        },
        scope:{
            iconName: '@'
        }
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36036232

复制
相关文章

相似问题

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