【问题标题】:SVG Text with Shadow in Internet ExplorerInternet Explorer 中带阴影的 SVG 文本
【发布时间】:2016-03-17 13:49:42
【问题描述】:

这是一个带有文本元素的简单 SVG 示例。为了便于阅读,我希望在文本周围有一个阴影。我已经使用 CSS 实现了这样的解决方案:text-shadow。

这里是文本元素:

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xml:space="preserve" version = "1.1" width= "100" height= "100">
   		<g>
   			<rect x="0" y="0" width="100" height="100" fill="gray"/>
    		<circle cx="50" cy="50" r="5"/>
    		<text x="50" y="40" style="text-shadow: -1px 0 white, 0 1px white, 1px 0 white, 0 -1px white;">Text</text>
    	</g>
</svg>

JSFiddle with SVG

这适用于 Chrome 和 Firefox。但阴影在 IE 中不可见。据我所知,IE 支持文本阴影,但不支持 SVG。是否有替代解决方案以获得类似的效果? 从 IE 9 开始会很好,但如果只有 10 或 11 也很好。

编辑:我正在寻找 SVG 解决方案。 SVG 用作地图的一部分,因此可以平移和缩放。所以使用 HTML 元素的解决方案并不是很有用。

【问题讨论】:

    标签: html css internet-explorer svg svg-filters


    【解决方案1】:

    您可以将单个 CSS 文本阴影替换为等效的 SVG 过滤器,格式为...

    <filter id="drop-shadow">
        <feGaussianBlur in="SourceAlpha" stdDeviation="[radius]"/>
        <feOffset dx="[offset-x]" dy="[offset-y]" result="offsetblur"/>
        <feFlood flood-color="[color]"/>
        <feComposite in2="offsetblur" operator="in"/>
        <feMerge>
            <feMergeNode/>
            <feMergeNode in="SourceGraphic"/>
        </feMerge>
    </filter>
    

    只需填写半径、offset-x、offset-y 和颜色值即可。

    您的 CSS 示例结合了四个偏移的非模糊阴影。等效的 SVG 过滤器可能看起来像...

    <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xml:space="preserve" version = "1.1" width= "100" height= "100">
        <defs>
            <filter id="dropShadow">
                <feFlood flood-color="white" result="flood"/>
                <feOffset dx="-1" dy="0" in="SourceAlpha" result="offset1"/>
                <feComposite operator="in" in="flood" in2="offset1" result="shadow1"/>
                <feOffset dx="0" dy="1" in="SourceAlpha" result="offset2"/>
                <feComposite operator="in" in="flood" in2="offset2" result="shadow2"/>
                <feOffset dx="1" dy="0" in="SourceAlpha" result="offset3"/>
                <feComposite operator="in" in="flood" in2="offset3" result="shadow3"/>
                <feOffset dx="0" dy="-1" in="SourceAlpha" result="offset4"/>
                <feComposite operator="in" in="flood" in2="offset4" result="shadow4"/>
                <feMerge>
                    <feMergeNode in="shadow1"/>
                    <feMergeNode in="shadow2"/>
                    <feMergeNode in="shadow3"/>
                    <feMergeNode in="shadow4"/>
                    <feMergeNode in="SourceGraphic"/>
                </feMerge>
            </filter>
        </defs>
        <g>
            <rect x="0" y="0" width="100" height="100" fill="gray"/>
            <circle cx="50" cy="50" r="5"/>
            <text x="50" y="40" filter="url(#dropShadow)">Text</text>
        </g>
    </svg>
    

    【讨论】:

    • 谢谢。此解决方案适用于 IE10 及更高版本。目前应该没问题。
    【解决方案2】:

    您可以从 svg 中删除文本元素并将其放入 DOM 文本元素(例如段落标签)并对其应用样式;像这样:

    <div class="svg-wrap">
      <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xml:space="preserve" version = "1.1" width= "100" height= "100">
        <g>
            <rect x="0" y="0" width="100" height="100" fill="gray"/>
            <circle cx="50" cy="50" r="5"/>
        </g>
      </svg>
      <p class="shadow">
      Text
      </p>
    </div>
    

    和css:

    .svg-wrap {
      position: relative;
    }
    .shadow {
      text-shadow: -1px 0 white, 0 1px white, 1px 0 white, 0 -1px white;
      position: absolute;
      top: 10px;
      left: 50px;
    }
    

    它正在运行:http://jsfiddle.net/Lyd69gr7/

    【讨论】:

    • 谢谢。那将是一个可行的解决方案。因为我在 SVG 中有很多不同位置的文本元素,所以会有点痛苦。另一方面,我可以将位置设置为内联样式,所以应该没问题。
    猜你喜欢
    • 2012-07-06
    • 1970-01-01
    • 2015-11-15
    • 2012-09-10
    • 1970-01-01
    • 1970-01-01
    • 2012-10-25
    • 1970-01-01
    • 2021-11-28
    相关资源
    最近更新 更多