我在我的项目中使用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。


当前回答

Use可以达到这个目的,但是那些被放置在Use help之后的元素很难操作…… 在我使用它之后,我不明白的是:为什么我不能在use元素上悬停(无论是鼠标悬停,鼠标进入操作都不能工作)来获得额外的功能-比如~在圆圈上显示文本~ 之后返回到圆圈重新排序,因为这是操作svg对象的唯一方式

其他回答

如前所述,svg按顺序呈现,(目前)不考虑z-index。也许只是把特定的元素发送到它的父元素的底部,这样它就会最后呈现。

function bringToTop(targetElement){
  // put the element at the bottom of its parent
  let parent = targetElement.parentNode;
  parent.appendChild(targetElement);
}

// then just pass through the element you wish to bring to the top
bringToTop(document.getElementById("one"));

为我工作。

更新

如果您有一个嵌套的SVG,其中包含组,则需要将项目从其parentNode中取出。

function bringToTopofSVG(targetElement){
  let parent = targetElement.ownerSVGElement;
  parent.appendChild(targetElement);
}

SVG的一个很好的特性是每个元素都包含它的位置,而不管它嵌套在哪个组中:+1:

通过transform:TranslateZ移动到前面

警告:仅适用于FireFox

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 160 160" style="width:160px;身高:160 px;" > <g style=" font - family:宋体;"> <g id="one" style=" font - family:宋体;"> <circle fill="green" cx="100" cy="105" r="20" style="transform:TranslateZ(1px);" > < /圆> < / g > <g id="two" style=" font - family:宋体;"> <圆填充= "橙色"残雪= " 100 " cy = " 95 " r = " 20 " > < /圆> < / g > < / g > < / svg >

规范

在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)

svg没有z指数。但是svg根据元素在DOM中的位置来确定它们的最上面。因此,您可以删除Object并将其放置到svg的末尾,使其成为“最后渲染”元素。然后将其呈现为视觉上的“最顶层”。


使用jQuery:

function moveUp(thisObject){
    thisObject.appendTo(thisObject.parents('svg>g'));
}

用法:

moveUp($('#myTopElement'));

使用D3.js:

d3.selection.prototype.moveUp = function() {
    return this.each(function() {
        this.parentNode.appendChild(this);
    });
};

用法:

myTopElement.moveUp();

只是想添加一个技巧,当你想把一个特定的元素放在上面。

function moveInFront(element) {
    const svg = element.closest('svg'); // Find the parent SVG
    svg.appendChild(element); // Append child moves the element to the end
}

这是因为(引用文档)“appendChild()将[元素]从当前位置移动到新位置”,而不是添加一个副本。

注意:如果元素是嵌套的,则必须在组内将元素移动到前面,也许还必须将组移动到前面。