【问题标题】:Why are my SVG marker and gradient defs being overwritten?为什么我的 SVG 标记和渐变定义被覆盖?
【发布时间】:2014-03-14 19:26:49
【问题描述】:

当我的 SVG 在我的网页中时,我的 SVG 的渐变和标记定义似乎被覆盖了。页面上没有其他 SVG。

但是当我将 SVG 从页面中取出时,它看起来还不错。

这是本例中的 SVG。

<svg  xmlns="http://www.w3.org/2000/svg" height="500" width="800">
  <style>
  svg .node.species {
    stroke: #FFB800;
    stroke-width: 3px;
    size: 300px;
  }
  svg .node.reaction {
    stroke: #8089F7;
    opacity: 0;
    stroke-width: 1.5px;
  }
  svg .link {
    stroke: black;
    stroke-width: 3px;
  }
  svg .link.modifier {
    stroke-dasharray: 5, 5;
  }
  svg .node-label {
    font-size: 14px;
    font-family: Georgia;
    font-weight: bolder;
    text-anchor: middle;
    dominant-baseline: middle;
  }
  /*  svg .node.selected {
    stroke: #FF0000;
  }
  svg .link.selected {
    stroke: #FF0000;
  }*/
  svg marker {
    overflow: visible;
  }
  svg .null-symbol {
    fill: none;
    stroke: black;
    stroke-width: 3px;
  }
  </style>
  <defs>
    <linearGradient id="gradient">
      <stop offset="5%" stop-color="#FFDC9E"></stop>
      <stop offset="95%" stop-color="#FFF"></stop>
    </linearGradient>
    <linearGradient id="markerGradient">
      <stop offset="5%" stop-color="rgb(97, 116, 255)"></stop>
      <stop offset="95%" stop-color="#FFF"></stop>
    </linearGradient>
    <linearGradient id="reactionGradient">
      <stop offset="5%" stop-color="#B0C0FF"></stop>
      <stop offset="95%" stop-color="#FFF"></stop>
    </linearGradient>
    <marker case-sensitive="refX,refY" id="production" viewBox="0 0 10 10"   markerWidth="10" markerHeight="10" orient="auto" refx="-2" refy="0" refX="-2" refY="0">
      <path fill="url(#markerGradient)" stroke="#0013FF" transform="rotate(-90)"  d="M0,2.0808957251439084L2.4028114141347543,-2.0808957251439084 -2.4028114141347543,-2.0808957251439084Z"></path>
    </marker>
    <marker case-sensitive="refX,refY" id="degradation" viewBox="0 0 10 10"   markerWidth="10" markerHeight="10" orient="auto" refx="-2" refy="0" refX="-2" refY="0">
      <path fill="url(#markerGradient)" stroke="#0013FF" transform="rotate(-90)"  d="M0,2.0808957251439084L2.4028114141347543,-2.0808957251439084 -2.4028114141347543,-2.0808957251439084Z"></path>
    </marker>
    <marker case-sensitive="refX,refY" id="modifier" viewBox="0 0 10 10" markerWidth="30" markerHeight="30"   orient="auto" refx="-0.4" refy="0" refX="-0.4" refY="0">
      <path stroke="black" stroke-width="0.3" fill="none"  d="M0,0.5641895835477563A0.5641895835477563,0.5641895835477563 0 1,1 0,-0.5641895835477563A0.5641895835477563,0.5641895835477563 0 1,1 0,0.5641895835477563Z"></path>
    </marker>
  </defs>
  <g  transform="translate(, )scale(1)">
    <g>
      <line    class="reaction production link"  marker-end="url(#production)" x1="449.9996697164325" y1="218.45791860388687" x2="451.42692033038736" y2="246.7481154723436">
      </line><line    class="reaction production link"  marker-end="url(#production)" x1="399.21583821133174" y1="309.0153067725509" x2="345.33989421522693" y2="338.2824980727582">
      </line>
    </g>
    <g>
      <line    class="reaction reactant link"     x1="446.9075572254284" y1="157.1677217354301" x2="449.9996697164325" y2="218.45791860388687">
      </line><line    class="reaction reactant link"     x1="453.0917822074366" y1="279.7481154723436" x2="399.21583821133174" y2="309.0153067725509">
      </line>
    </g>

    <g>

    </g>
    <g draggable=""   transform="translate(446.9075572254284,157.1677217354301)" style="position: relative; border: 1px solid red; background-color: rgb(211, 211, 211); cursor: pointer;">
      <rect  class="species node"      fill="url(#gradient)"  x="-50" y="-15" width="100" height="30" ry="15">
        <title>ID: S1, Name: S1</title>
      </rect>
      <text class="node-label">S1</text>
    </g><g draggable=""   transform="translate(453.0917822074366,279.7481154723436)" style="position: relative; border: 1px solid red; background-color: rgb(211, 211, 211); cursor: pointer;">
      <rect  class="species node"      fill="url(#gradient)"  x="-50" y="-15" width="100" height="30" ry="15">
        <title>ID: S2, Name: S2</title>
      </rect>
      <text class="node-label">S2</text>
    </g><g draggable=""   transform="translate(345.33989421522693,338.2824980727582)" style="position: relative; border: 1px solid red; background-color: rgb(211, 211, 211); cursor: pointer;">
      <rect  class="species node"      fill="url(#gradient)"  x="-50" y="-15" width="100" height="30" ry="15">
        <title>ID: S3, Name: S3</title>
      </rect>
      <text class="node-label">S3</text>
    </g>
    <g draggable=""   transform="translate(473.4770117242872,216.74146493626216)" style="position: relative; border: 1px solid red; background-color: rgb(211, 211, 211); cursor: pointer;">
      <circle  class="node reaction"  fill="url(#reactionGradient)"  r="5">
        <title>ID: reaction1, Name: reaction1</title>
      </circle>
      <text class="node-label"></text>
    </g><g draggable=""   transform="translate(409.9633485910613,329.9478703486097)" style="position: relative; border: 1px solid red; background-color: rgb(211, 211, 211); cursor: pointer;">
      <circle  class="node reaction"  fill="url(#reactionGradient)"  r="5">
        <title>ID: reaction2, Name: reaction2</title>
      </circle>
      <text class="node-label"></text>
    </g>
  </g>
</svg>

编辑 1

这是linearGradient元素的样式

编辑 2

defs 元素的样式

编辑 3

我发现当我删除 head 标记,然后重新应用它时,SVG 会正确呈现。很奇怪。

【问题讨论】:

  • 我知道这样的行为。所以 linearGradient 元素具有“display:none”属性,那么使用这个渐变的填充就被破坏了。请检查渐变和标记元素的样式。
  • @defghi1977,我添加了linearGradient的风格截图,我没有立即看到任何奇怪的东西。计算出的显示样式为inline
  • 当 linearGradient 的祖先元素具有 display:none 属性时会出现此行为。所以,如果 defs 元素有这个属性,linearGradient 也会被破坏。请亲眼检查css结构。
  • @defghi1977,我明白你的意思,当我在独立 SVG 上切换 display:none 时,我能够重现此行为。但奇怪的是,当我检查嵌入式 SVG 时,没有 display:none,当我手动将其切换为 display:inline 时,修复它也没有任何效果。关于在哪里寻找可能发生这种情况的任何想法?
  • 似乎没有任何影响它的 CSS。但是我发现当我删除 HEAD 元素然后重新添加它时,SVG 显示正确呈现。

标签: svg


【解决方案1】:

原来我在head 中有&lt;base href="/"&gt;,这把事情搞砸了。

【讨论】:

  • 非常感谢您回来提供这个答案。我花了好几个小时研究这个问题,你的回答终于帮我解决了。
猜你喜欢
  • 1970-01-01
  • 2020-11-14
  • 2020-11-06
  • 1970-01-01
  • 2011-09-22
  • 2016-08-27
  • 1970-01-01
  • 2019-03-03
  • 2015-07-31
相关资源
最近更新 更多