我在我的项目中使用svg圆圈,像这样,

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 160 120">
    <g>
        <g id="one">
            <circle fill="green" cx="100" cy="105" r="20" />
        </g>
        <g id="two">
            <circle fill="orange" cx="100" cy="95" r="20" />
        </g>
    </g>
</svg>

我在g标签中使用z索引来显示第一个元素。在我的项目中,我只需要使用z-index值,但我不能使用我的svg元素的z-index。我在谷歌上搜了很多,但没有找到相关的东西。 所以请帮助我在我的svg中使用z-index。

这里是DEMO。


当前回答

使用D3:

如果你想以相反的顺序将元素添加到数据中,使用:

.insert(“g”、”:第一个孩子”)

而不是。append('g')

将一个元素添加到组元素的顶部

其他回答

正如其他人所说,z-index是由元素在DOM中出现的顺序定义的。如果手动重新排序你的html不是一个选项或将是困难的,你可以使用D3重新排序SVG组/对象。

使用D3更新DOM顺序和模仿Z-Index功能

使用D3更新SVG元素Z-Index

在最基本的级别上(如果您没有将id用于其他任何事情),您可以使用元素id作为z-index的替代品,并使用它们进行重新排序。除此之外,你几乎可以让你的想象力自由发挥。

代码片段中的示例

var circles = d3.selectAll('circle') var label = d3.select('svg').append('text') .attr('transform', 'translate(' + [5,100] + ')') var zOrders = { IDs: circles[0].map(function(cv){ return cv.id; }), xPos: circles[0].map(function(cv){ return cv.cx.baseVal.value; }), yPos: circles[0].map(function(cv){ return cv.cy.baseVal.value; }), radii: circles[0].map(function(cv){ return cv.r.baseVal.value; }), customOrder: [3, 4, 1, 2, 5] } var setOrderBy = 'IDs'; var setOrder = d3.descending; label.text(setOrderBy); circles.data(zOrders[setOrderBy]) circles.sort(setOrder); <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 400 100"> <circle id="1" fill="green" cx="50" cy="40" r="20"/> <circle id="2" fill="orange" cx="60" cy="50" r="18"/> <circle id="3" fill="red" cx="40" cy="55" r="10"/> <circle id="4" fill="blue" cx="70" cy="20" r="30"/> <circle id="5" fill="pink" cx="35" cy="20" r="15"/> </svg>

基本思想是:

Use D3 to select the SVG DOM elements. var circles = d3.selectAll('circle') Create some array of z-indices with a 1:1 relationship with your SVG elements (that you want to reorder). Z-index arrays used in the examples below are IDs, x & y position, radii, etc.... var zOrders = { IDs: circles[0].map(function(cv){ return cv.id; }), xPos: circles[0].map(function(cv){ return cv.cx.baseVal.value; }), yPos: circles[0].map(function(cv){ return cv.cy.baseVal.value; }), radii: circles[0].map(function(cv){ return cv.r.baseVal.value; }), customOrder: [3, 4, 1, 2, 5] } Then, use D3 to bind your z-indices to that selection. circles.data(zOrders[setOrderBy]); Lastly, call D3.sort to reorder the elements in the DOM based on the data. circles.sort(setOrder);


例子

可以按ID进行堆叠


最左边的SVG在上面


上面的半径最小


或者指定一个数组来为特定的顺序应用z-index——在我的示例代码中,数组[3,4,1,2,5]移动/重新排序第3个圆(在原始HTML顺序中),使其在DOM中处于第1位,第4位为第2位,第1位为第3位,以此类推……


另一种解决方案是使用div,它使用zIndex来包含SVG元素。在这里: https://stackoverflow.com/a/28904640/4552494

一个更好的例子,我已经用过了。

<svg>
  <defs>
    <circle id="one" fill="green" cx="40" cy="40" r="20" /> 
    <circle id="two" fill="orange" cx="50" cy="40" r="20"/> 
  </defs>
  <use href="#two" />
  <use href="#one" />
</svg>

为了控制顺序,你可以改变这些use元素的href属性值。这对动画制作很有用。

多亏了defs,圆圈元素只绘制一次。

jsfiddle.net/7msv2w5d

你可以利用利用。

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 160 120">
    <g>
        <g id="one">
            <circle fill="green" cx="100" cy="105" r="20" />
        </g>
        <g id="two">
            <circle fill="orange" cx="100" cy="95" r="20" />
        </g>
    </g>
    <use xlink:href="#one" />
</svg>

绿色圆圈出现在顶部。 jsFiddle

规范

在SVG规范1.1版本中,呈现顺序是基于文档顺序的:

first element -> "painted" first

参考SVG 1.1。规范

3.3渲染顺序 SVG文档片段中的元素有一个隐式的绘制顺序,SVG文档片段中的第一个元素首先“绘制”。后续的元素被绘制在先前绘制的元素之上。


解决方案(cleaner-faster)

你应该把绿色的圆圈作为最近要绘制的对象。交换这两个元素。

<svg xmlns="http://www.w3.org/2000/svg" viewBox="30 70 160 120"> <!首先画一个橙色的圆——> <circle fill="orange" cx="100" cy="95" r="20"/> <!——然后在当前画布上绘制绿色圆圈——> <圆填充="绿色" cx="100" cy="105" r="20"/> < / svg >

这里是jsFiddle的fork。

解决方案(选择)

该标记与属性xlink:href(对于SVG 2只是href)一起使用,并将元素的id作为值。请记住,这可能不是最好的解决方案,即使结果看起来很好。有一点时间,这里是规范SVG 1.1“use”元素的链接。

目的: 避免要求作者修改所引用的文档以向根元素添加ID。

<svg xmlns="http://www.w3.org/2000/svg" viewBox="30 70 160 120"> <!——首先画一个绿色的圆——> <圆id =“1”填补=“绿色”残雪= " 100 " cy = " 105 " r = " 20 " / > <!——然后在当前画布上绘制橙色圆圈——> <圆id = "两个“填补=“橙色cx”=“100”cy = " 95 " r = " 20 " / > <!——最后在当前画布上再次绘制绿色圆圈——> <使用xlink: href = " # 1 " / > < / svg >


关于SVG 2的说明

SVG 2规范是下一个主要版本,仍然支持上述特性。

3.4. Rendering order Elements in SVG are positioned in three dimensions. In addition to their position on the x and y axis of the SVG viewport, SVG elements are also positioned on the z axis. The position on the z-axis defines the order that they are painted. Along the z axis, elements are grouped into stacking contexts. 3.4.1. Establishing a stacking context in SVG ... Stacking contexts are conceptual tools used to describe the order in which elements must be painted one on top of the other when the document is rendered, ...

SVG 2支持Mozilla -绘画 我如何知道我的浏览器是否支持svg 2.0 我可以使用SVG吗? 对于SVG 2,使用href代替额外的已弃用名称空间XLink:href(感谢G07cha)