【问题标题】:SVG 'use' tag in Chrome brokenChrome中的SVG“使用”标签损坏
【发布时间】:2016-07-02 08:46:31
【问题描述】:

有由 svg-sprite 制作的带有基于图标的导航的 SAP(AngularJS 和 Angular Route)。所以,我有这样的内联代码:

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

导航中的图标是这样的:

<a href=""><svg class="icon icon-32 outline black"><use xlink:href="#icon-grid-32"></use></svg></a>

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

在 Firefox、Safari 等浏览器中有效,但在 Chrome 49+ 中无效(48 版本不存在此问题)。

【问题讨论】:

标签: angularjs google-chrome svg


【解决方案1】:

我遇到了与您描述的问题非常相似的问题,不同之处在于我会在指令中生成我的图标 &lt;svg&gt;&lt;use&gt;

我一直在寻找今天大部分时间的答案,并想出了解决 &lt;use&gt;xlink:href 问题的方法。它只是通过内联想要的 svg 来重新创建功能。

为了简单起见,假设我有&lt;angular-icon&gt; 指令,它通过属性icon-name 接收图标的名称

&lt;angular-icon icon-name="{{someObject.iconName}}"&gt;

工作指令现在如下所示:

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

【讨论】:

    【解决方案2】:

    这是由 AngularJS 对 &lt;base href="/" /&gt; 的依赖和 UI 路由的组合造成的,当应用程序未处于其“根”状态时,&lt;use&gt; 元素中的相对哈希链接将无法正确解析。

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

    angular-icon-template.html

    <svg class="c-icon" viewBox="0 0 32 32">
        <use xlink:href="" ng-attr-xlink:href="{{baseUrl + '#' + iconName}}" />
    </svg>
    

    angular-icon.js

    angular.module('angularIcon', [])
        .directive('angularIcon', {
            templateUrl: 'angular-icon-template.html',
            scope: { iconName: '@' },
            controller: function($scope) {
                $scope.baseUrl = window.location.href.replace(window.location.hash, '');
            }
        });
    

    【讨论】:

    • 你救了我的头发。
    猜你喜欢
    • 1970-01-01
    • 2017-06-26
    • 1970-01-01
    • 2013-07-10
    • 1970-01-01
    • 1970-01-01
    • 2014-03-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多