简体   繁体   English

嵌套svg的D3.js缩放会中断Internet Explorer中的视口

[英]D3.js zoom with nested svg breaks viewport in Internet Explorer

I am using d3.js to dynamically set up a nested svg, ie an inner svg nested inside an enclosing svg. 我正在使用d3.js动态设置嵌套的svg,即嵌套在封闭的svg中的内部svg。 A d3.behavior.zoom() listens for zoom events on the outer svg and does the transformations needed. d3.behavior.zoom()侦听外部svg上的缩放事件,并进行所需的转换。

All works fine except for the Internet Explorer (IE 11) which seems to have an issue with transformations involving inner svgs. 除了Internet Explorer(IE 11)似乎与涉及内部svg的转换有关的问题外,其他所有工具都工作正常。 Both Firefox and Chrome behave as expected clipping the inner svg to the viewport of the outer svg. Firefox和Chrome的行为均符合预期,将内部svg剪切到外部svg的视口中。 In Internet Explorer, however, zooming in correctly applies transformations but seems to ignore the dimensions of the enclosing svg. 但是,在Internet Explorer中,放大正确地应用了转换,但似乎忽略了封闭的svg的尺寸。 The contents of the inner svg will eventually be displayed outside of the outer svg and above other body elements. 内部svg的内容最终将显示在外部svg的外部以及其他主体元素上方。 The viewport of the outer svg seems to have no clipping effect on the inner svg. 外部svg的视口似乎对内部svg没有修剪效果。

I have set up a JSFiddle demonstrating the behaviour. 我已经建立了一个JSFiddle来演示行为。

var zoom = d3.behavior.zoom()
    .on("zoom", function () {
        container.attr("transform",
            "translate(" + d3.event.translate + ") " +
            "scale(" + d3.event.scale + ")");
    });

var container = d3.select("body")
                    .append("svg")
                      .attr("id", "svgcontainer")
                      .attr("width", 300)
                      .attr("height", 300)
                      .style("background-color", "#aaaaee")
                      .call(zoom)
                    .append("g");

var svg = container.append("svg")
                     .attr("width", 200)
                     .attr("height", 200)
                     .attr("x", 50)
                     .attr("y", 50);

svg.append("svg:circle")
     .style("fill", "none")
     .style("stroke", "red")
     .style("stroke-width", "2px")
     .attr("cx", 100)
     .attr("cy", 100)
     .attr("r", 50);

Am I missing something? 我想念什么吗? Is there any cross-browser workaround? 有跨浏览器的解决方法吗?

I'm sorry this question didn't get enough attention when you first posted it: it's actually a simple fix. 抱歉,当您首次发布该问题时,这个问题没有引起足够的重视:这实际上是一个简单的解决方法。 Just set the overflow property on the outer SVG to hidden . 只需将外部SVG的overflow属性设置为hidden

So why does your code work as you intend on the other browsers? 那么,为什么您的代码可以在其他浏览器上正常工作呢?

It's because they set this property by default. 这是因为他们默认设置了此属性。 The initial value for overflow in CSS is visible , but the SVG specs require that any element that can take a viewBox attribute has overflow:hidden in the browser's default stylesheet, except for the root SVG element. CSS overflow的初始值是visible ,但是SVG规范要求, 除了根SVG元素以外 ,任何可以采用viewBox属性的元素都必须在浏览器的默认样式表中具有overflow:hidden viewBox The other browsers interpret this exception as if it only applied to an <svg> element that is the root of an .svg document. 其他浏览器将此异常解释为仅适用于作为.svg文档根的<svg>元素。 Internet Explorer also applies treats the top-level inline SVG in an HTML document as if was a root (and therefore had overflow: visible ). Internet Explorer还应用将HTML文档中的顶级内联SVG视为根目录(因此有overflow: visible )。

The following snippet demonstrates the different behaviors. 以下代码段演示了不同的行为。 It uses a circle inside a nested SVG inside an inline SVG. 它在嵌入式SVG内的嵌套SVG内使用一个圆圈。 The circle is too big for the nested SVG, so if overflow is hidden on the nested SVG (as it is by default in all browsers) the circle will be cropped to a square. 该圆形对于嵌套的SVG太大,因此,如果嵌套SVG上隐藏了溢出(所有浏览器默认情况下),则该圆形将被裁剪为一个正方形。 The nested SVG is offset, partly outside the outer SVG. 嵌套的SVG偏移,部分在外部SVG外部。 If overflow is hidden on the outer SVG, the nested SVG will be cropped to a rectangle; 如果外部SVG上隐藏了溢出,则嵌套的SVG将被裁剪为一个矩形; if overflow is visible you'll see the square sticking outside of the frame. 如果可见溢出,您会看到正方形粘在框架外部。

The first SVG uses default overflow on the outer SVG (different for IE) while the others explicitly set overflow: hidden or overflow: visible . 第一个SVG在外部SVG上使用默认溢出(对于IE而言是不同的),而其他SVG则明确设置了overflow: hiddenoverflow: visible

 svg { border: solid gray; height: 100px; width: 100px; margin: 50px; } circle { fill: royalBlue; } 
 <svg> <svg x="-50" y="-50" width="100" height="100" > <circle r="100" cx="50" cy="50"/> </svg> </svg> <svg style="overflow: hidden"> <svg x="-50" y="-50" width="100" height="100" > <circle r="100" cx="50" cy="50"/> </svg> </svg> <svg style="overflow: visible"> <svg x="-50" y="-50" width="100" height="100" > <circle r="100" cx="50" cy="50"/> </svg> </svg> 

The overflow behavior should probably be clarified for SVG 2 or in the SVG integration spec . 对于SVG 2SVG集成规范,应该阐明溢出行为。 There is also a discrepancy between Firefox and Blink/Webkit browsers regarding whether or not padding on an inline SVG is considered "overflow" or not. Firefox和Blink / Webkit浏览器之间在是否将嵌入式SVG上的填充视为“溢出”方面也存在差异。

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

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