隐藏SVG会影响同一页面中的其他SVG样式

Dav*_*las 5 html css svg display

SVG在同一页面中加载了几次。SVG用于显示值的图形表示。想象一下一个地图,其中每个区域都使用颜色代码显示给定值。

在每个SVG中,对于每个区域,都会动态应用CSS类以匹配所需的SVG模式填充。

CSS样式和模式在SVG文件中定义。这是一个例子:

  <svg height="100" width="100">
    <style>
    /*  */
    .striped-pain-1 {fill: url(#striped-pain-1);}
    /*  */
    </style>
    <defs>
        <pattern id="striped-pain-1" width="4" height="1" patternTransform="rotate(45 0 0)" patternUnits="userSpaceOnUse">
            <line x1="0" y1="0" x2="0" y2="2" style="stroke:#EABFD5; stroke-width:6"></line>
        </pattern>
    </defs>
Run Code Online (Sandbox Code Playgroud)

问题是(display:none例如)隐藏了其中的SVG时(从那里到页面底部),所有的SVG都松散了图案填充。

我做了一个简化的Plunker来显示问题。

https://plnkr.co/edit/F5TzOwDEzneHEW7PT3Ls?p=preview

我发现防止这种情况的唯一方法是ids为每个SVG 使用不同的模式,但是由于所有人都共享同一个文件,因此我不喜欢重复复制所有文件并为此重命名ID的解决方案。我想知道应该有更好的解决方案。

enx*_*eta 5

我会将模式放在另一个svg元素中<svg class="defs">。该svg元素可能具有position:absolute非常小的宽度和高度。如果添加left: -200px;此svg元素是不可见的。

另外:如果一个SVG元素具有此CSS规则:.striped-pain-1 {fill: url(#striped-pain-1);}您无需将其添加到第二个。实际上,您可以<style>从svg中删除该元素,然后将此规则添加到css中。

请尝试:单击数字(1,2,3)隐藏或取消隐藏svg元素。

let spans = Array.from(document.querySelectorAll("#commands span"))
let svgs = Array.from(document.querySelectorAll(".svgcontainer"))
spans.forEach((s,i) =>{
let n = 0;
s.addEventListener("click",(e)=>{
n++;
let thisSvg = svgs[i].querySelector("svg")
if(n%2 == 1){thisSvg.style.display="none";
            }else{
             thisSvg.style.display="block";}
})
})
Run Code Online (Sandbox Code Playgroud)
svg {
  display:block;
}
.defs {
  position: absolute;
  left: -200px;
}
span {
  display: inline-block;
  width: 2em;
  height: 1em;
  border: 1px solid;
  text-align: center;
  cursor: pointer;
}
.svgcontainer {
  height: 100px;
  width: 100px;
  border: 1px solid;
  display: inline-block;
}
Run Code Online (Sandbox Code Playgroud)
<p id="commands"><span>1</span> <span>2</span> <span>3</span></p>

<svg class="defs" width="1" height="1">
  <defs>
  		<pattern id="striped-pain-1" width="4" height="1" patternTransform="rotate(45 0 0)" patternUnits="userSpaceOnUse">
  			<line x1="0" y1="0" x2="0" y2="2" style="stroke:#EABFD5; stroke-width:6"></line>
  		</pattern>
    
      <pattern id="striped-pain-2" width="4" height="1" patternTransform="rotate(45 0 0)" patternUnits="userSpaceOnUse">
    		<line x1="0" y1="0" x2="0" y2="2" style="stroke:#EABFD5; stroke-width:6"></line>
    	</pattern>
  	</defs>
</svg>

<div class="svgcontainer">
  <svg height="100" width="100">
  	<style>
  		/*  */
  		.striped-pain-1 {fill: url(#striped-pain-1);}
  		/*  */
  	</style>
    
    <circle class="striped-pain-1" cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" />
  </svg> 

</div>
<div class="svgcontainer">
  <svg height="100" width="100">
   <!-- <style>
    		/*  */
    		.striped-pain-1 {fill: url(#striped-pain-1);}
    		/*  */
    </style>-->
    <!--<defs>
    	<pattern id="striped-pain-1" width="4" height="1" patternTransform="rotate(45 0 0)" patternUnits="userSpaceOnUse">
    		<line x1="0" y1="0" x2="0" y2="2" style="stroke:#EABFD5; stroke-width:6"></line>
    	</pattern>
    </defs>-->
    <circle class="striped-pain-1" cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" />
  </svg> 
</div>
<div class="svgcontainer">
  <svg height="100" width="100">
    <style>
    		/*  */
    		.striped-pain-2 {fill: url(#striped-pain-2);}
    		/*  */
    </style>
    <circle class="striped-pain-2" cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" />
  </svg> 
</div>
Run Code Online (Sandbox Code Playgroud)

  • 这就是我最终使用的方法。我只是在一个不可见的`svg`元素中共享所有svg的所有`defs'。由于避免了不必要的重复,这也简化了我的代码。 (2认同)