【问题标题】:The nested in foreignobject svg does not appear嵌套在foreignobject svg中没有出现
【发布时间】:2018-06-11 16:16:16
【问题描述】:

嵌套在foreignobject svg 中没有出现。我在这里做错了什么?

   

var svg = d3.select("#drawRegion")
  .append("svg")
  .attr("width", "100%")
  .attr("height", "100%");
svg.append("rect")
  .attr("x", "0")
  .attr("y", "0")
  .attr("width", "100%")
  .attr("height", "100%")
  .attr("fill", "yellow");

var fObj = svg.append("foreignobject");
fObj
  .attr("x", "10%")
  .attr("y", "10%")
  .attr("width", "80%")
  .attr("height", "80%")
  .attr("viewBox", "0 0 80 80")
  .attr("preserveAspectRatio", "xMidYMin slice");
  
  var scrollDiv = fObj.append("div");
  scrollDiv
  .style("width", "100%")
  .style("height", "100%")
  .style("overflow", "scroll");
  
  var scrollSvg = scrollDiv
  .append("svg");
  
  scrollSvg
  .attr("x", 0)
	.attr("y", 0)
  .attr("width", "500%")
  .attr("height", "500%");
  
  var rectP = scrollSvg
  .append("rect");
  
  rectP
  .attr("x", 0)
	.attr("y", 0)
  .attr("width", "100%")
  .attr("height", "100%")
  .attr("fill", "cyan");
<div id="drawRegion">

</div>
<script src="https://d3js.org/d3.v5.min.js"></script>

  
</svg>

我期待添加主要的svg -> foreignobject -> div -> svg。通过这样做,我希望得到一个嵌套的可滚​​动svg 元素。但由于某些原因,以foreignobject 开头的所有内容都不会显示。我不知道该尝试什么。

检查控制台后,我没有发现任何错误。

Here 是一个 jsfiddle。

【问题讨论】:

  • @RobertLongson,没有。这并没有帮助。据我所知,svg 并不关心foreignobject 中的情况。例如here.

标签: html d3.js svg nested scrollable


【解决方案1】:

它是外来对象,而不是外来对象(SVG 区分大小写)。

d3 还要求 html 标签以 xhtml 为前缀:

var svg = d3.select("#drawRegion")
  .append("svg")
  .attr("width", "100%")
  .attr("height", "100%");
svg.append("rect")
  .attr("x", "0")
  .attr("y", "0")
  .attr("width", "100%")
  .attr("height", "100%")
  .attr("fill", "yellow");

var fObj = svg.append("foreignObject");
fObj
  .attr("x", "10%")
  .attr("y", "10%")
  .attr("width", "80%")
  .attr("height", "80%")
  .attr("viewBox", "0 0 80 80")
  .attr("preserveAspectRatio", "xMidYMin slice");
  
  var scrollDiv = fObj.append("xhtml:div");
  scrollDiv
  .style("width", "100%")
  .style("height", "100%")
  .style("overflow", "scroll");
  
  var scrollSvg = scrollDiv
  .append("svg");
  
  scrollSvg
  .attr("x", 0)
	.attr("y", 0)
  .attr("width", "500%")
  .attr("height", "500%");
  
  var rectP = scrollSvg
  .append("rect");
  
  rectP
  .attr("x", 0)
	.attr("y", 0)
  .attr("width", "100%")
  .attr("height", "100%")
  .attr("fill", "cyan");
#drawRegion {
  width: 100%;
  height: 100%;
  display: inline-block;
  position: fixed;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  margin: auto;
  position: relative;
}
<div id="drawRegion">

</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>

【讨论】:

  • 哦!!!太好了谢谢。唯一的问题是仍然无法滚动。你知道如何解决吗?请帮帮我。
  • 很可惜。似乎不可能完成我想要的。在here.
  • 使用没有 Chrome 的 foreignObject 漏洞的 Firefox。
  • 还有其他方法可以使嵌套的svg 可滚动吗?
  • 自己实现。绘制滚动条,处理鼠标点击,根据鼠标点击翻译内容,移动滚动条抓取器等。
猜你喜欢
  • 2017-03-18
  • 1970-01-01
  • 2016-12-02
  • 2012-06-27
  • 1970-01-01
  • 1970-01-01
  • 2016-09-23
  • 2013-03-06
  • 2014-03-23
相关资源
最近更新 更多