简体   繁体   English

隐藏svg元素的最佳做法

[英]Best practice to hide a svg element

I do have many (>100) SVG- <g> -elements on my screen, and if the user hovers over one of them, inner img-elements included in this <g> should become visible. 我的屏幕上确实有许多(> 100)SVG- <g>元素,如果用户将鼠标悬停在其中之一上,则该<g>包含的内部img元素应该会变为可见。 If the user hovers out of the <g> , they should turn invisible again. 如果用户将鼠标悬停在<g>上方,则他们应该再次变为不可见。

I see there are two ways of doing this: 我看到有两种方法可以做到这一点:

  1. Insert the <img> on every mouseenter and remove it on mouseleave. 在每个mouseent上插入<img> ,并在mouseleave上将其删除。
  2. Insert the elements on every <g> during the initial rendering, make them initially invisible and turn them visible on mouseenter. 在初始渲染期间,将元素插入每个<g> ,使其最初不可见,并在mouseenter上将其变为可见。

Which one is the way to go? 该走哪条路? If it is #2, whats the CSS property to use? 如果是#2,则使用什么CSS属性? I found many, including 我发现了很多,包括

visibility : hidden;

and

display : none;

Keep in mind that they doesn't work in the same way, 请记住,它们的工作方式不同,

visibility: hidden;

Conserve the space that the object is using, so if your object size is for example: Width: 250px, Height:200px, will show a blank space. 保留对象正在使用的空间,因此,如果您的对象大小为例如:宽度:250像素,高度:200像素,将显示空白。

By the other hand: 另一方面:

display:none;

won't show the blank space, your object still be there but at Width:0px Height:0px 将不显示空格,您的对象仍然存在,但位于Width:0px Height:0px

Bye! 再见!

Either will do. 都可以。 There is not effective difference between them when it comes to SVG. 在SVG方面,它们之间没有有效的区别。

There is one exception, and that is when they are used with <tspan> elements. 有一个例外,那就是当它们与<tspan>元素一起使用时。 If a <tspan> is visibility: hidden it will leave a gap. 如果<tspan>visibility: hidden它会留一个空白。 But it will be completely ignored with dispay: none . 但是将被dispay: none完全忽略dispay: none

For every other use, the behaviour will be the same, and you can use either. 对于其他用途,其行为将是相同的,您可以使用其中一种。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM