简体   繁体   English

是什么导致SVG图片在调整大小后消失?

[英]What causes svg image to disapear on resize?

I have a project I am working on where I am initially displaying one portion of an svg file. 我有一个项目正在处理,我最初显示svg文件的一部分。 When a user clicks a switch button, it hides that portion of the svg file and displays another portion of the same svg. 当用户单击切换按钮时,它将隐藏svg文件的该部分并显示同一svg的另一部分。 I have written a script that resizes the image based upon the size of the window so the image always fits the width of the window. 我编写了一个脚本,该脚本根据窗口的大小调整图像的大小,以便图像始终适合窗口的宽度。 When I resize the initially viewed portion of the svg, it does fine. 当我调整svg最初查看的部分的大小时,它确实很好。

However, when I swap to the alternate portion of the svg file, it disappears upon resize. 但是,当我交换到svg文件的备用部分时,它在调整大小后会消失。 I'm not sure what is causing it. 我不确定是什么原因造成的。 Why does my code work for one portion of the svg file and not for the other portion? 为什么我的代码只能用于svg文件的一部分,而不能用于另一部分?

function setSVGheight(){
  var svgId = document.getElementsByTagName("svg")[0].id;
  var svgtop = document.getElementById("fullstaff").getBoundingClientRect().top;
  var svgbottom = document.getElementById("fullstaff").getBoundingClientRect().bottom;
  var svgheight = ((svgbottom-svgtop)*1.6).toFixed(3);
  document.getElementById(svgId).style.height=svgheight;
}
function exchangestaffandtab(exchangestate){
  if (exchangestate=="s2t"){
      document.getElementById("t2s").style.display="inline";
      document.getElementById("s2t").style.display="none";
      document.getElementById("fullstaff").style.display="none";
      document.getElementById("fullTAB").style.display="block";
  }else if(exchangestate=="t2s"){
      document.getElementById("t2s").style.display="none";
      document.getElementById("s2t").style.display="inline";
      document.getElementById("fullstaff").style.display="block";
      document.getElementById("fullTAB").style.display="none";
  }
}

http://jsfiddle.net/slayerofgiants/d48xJ/6/ I have a jsfiddle which semi-emulates the behavior. http://jsfiddle.net/slayerofgiants/d48xJ/6/我有一个jsfiddle可以半模仿行为。 The 2nd image clearly disappears after resize though my resize function is supposed to collapse the whitespace to the height of the drawing which the jsfiddle is not doing. 调整大小后,第二张图片显然消失了,尽管我的调整大小功能应该将空白折叠到jsfiddle不执行的绘图的高度。 Thanks, --christopher 谢谢--christopher

I discovered that in my switch function that when I set the display to none that the element no longer had a calculable height. 我发现在开关功能中,当我将显示设置为无时,该元素不再具有可计算的高度。 So it was getting set to 0 height. 因此将其设置为0高度。 I modified the setSVGheight function to include the following. 我修改了setSVGheight函数以包括以下内容。

function setSVGheight(){
  var svgId = document.getElementsByTagName("svg")[0].id;
  if (document.getElementById("fullstaff").style.display != "block"){
      document.getElementById("fullstaff").style.display = "block";
      var staffnone = true;
  }
  var svgtop = document.getElementById("fullstaff").getBoundingClientRect().top;
  var svgbottom = document.getElementById("fullstaff").getBoundingClientRect().bottom;

  var svgheight = ((svgbottom-svgtop)*1.6).toFixed(3);
  document.getElementById(svgId).style.height=svgheight;

  if( staffnone == true){
      document.getElementById("fullstaff").style.display = "none";
  }

} }

I checked to see if the portion used to measure height was not displayed as a block. 我检查了一下用于测量高度的部分是否未显示为块。 I then set it to block, calculated the height and then reset to none. 然后,将其设置为块,计算高度,然后将其重置为无。

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

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