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


当前回答

这是关于z-index和svg搜索的顶级谷歌结果。看完所有的答案后,有些答案很好,但我还是很困惑。

因此,对于像我这样的新手来说,这是目前的总结,9年后的2022年。

svg不能用z指数。

在svg中,z-index由元素在文档中出现的顺序定义。

如果你想要一些东西出现在顶部,或更接近用户,最后绘制或插入它之前。源

SVG 2可以支持z-index,但可能永远不会出现

SVG 2是一个实现这些功能和其他功能的提议,但它面临着永远无法向前发展的风险。

SVG 2于2016年进入候选推荐阶段,并于2018年进行了修订,最新草案于2021年6月8日发布。源

然而,它并没有得到很多的支持,很少有人在做这件事。所以不要屏住呼吸等这个消息。

你可以使用D3,但可能不应该

通常用于可视化数据的D3通过绑定你的z-index,然后排序来支持z-index,但它是一个大而复杂的库,如果你只是想让某个SVG出现在堆栈顶部,它可能不是最好的选择。

其他回答

试着颠倒# 1和# 2。看看这个小提琴:http://jsfiddle.net/hu2pk/3/

更新

在SVG中,z-index由元素在文档中出现的顺序定义。如果你愿意,你也可以看看这个页面:https://stackoverflow.com/a/482147/1932751

这是关于z-index和svg搜索的顶级谷歌结果。看完所有的答案后,有些答案很好,但我还是很困惑。

因此,对于像我这样的新手来说,这是目前的总结,9年后的2022年。

svg不能用z指数。

在svg中,z-index由元素在文档中出现的顺序定义。

如果你想要一些东西出现在顶部,或更接近用户,最后绘制或插入它之前。源

SVG 2可以支持z-index,但可能永远不会出现

SVG 2是一个实现这些功能和其他功能的提议,但它面临着永远无法向前发展的风险。

SVG 2于2016年进入候选推荐阶段,并于2018年进行了修订,最新草案于2021年6月8日发布。源

然而,它并没有得到很多的支持,很少有人在做这件事。所以不要屏住呼吸等这个消息。

你可以使用D3,但可能不应该

通常用于可视化数据的D3通过绑定你的z-index,然后排序来支持z-index,但它是一个大而复杂的库,如果你只是想让某个SVG出现在堆栈顶部,它可能不是最好的选择。

这很容易做到:

克隆你的物品 对克隆项进行排序 替换克隆项

function rebuildElementsOrder( selector, orderAttr, sortFnCallback ) { let $items = $(selector); let $cloned = $items.clone(); $cloned.sort(sortFnCallback != null ? sortFnCallback : function(a,b) { let i0 = a.getAttribute(orderAttr)?parseInt(a.getAttribute(orderAttr)):0, i1 = b.getAttribute(orderAttr)?parseInt(b.getAttribute(orderAttr)):0; return i0 > i1?1:-1; }); $items.each(function(i, e){ e.replaceWith($cloned[i]); }) } $('use[order]').click(function() { rebuildElementsOrder('use[order]', 'order'); /* you can use z-index property for inline css declaration ** getComputedStyle always return "auto" in both Internal and External CSS decl [tested in chrome] rebuildElementsOrder( 'use[order]', null, function(a, b) { let i0 = a.style.zIndex?parseInt(a.style.zIndex):0, i1 = b.style.zIndex?parseInt(b.style.zIndex):0; return i0 > i1?1:-1; }); */ }); use[order] { cursor: pointer; } <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" id="keybContainer" viewBox="0 0 150 150" xml:space="preserve"> <defs> <symbol id="sym-cr" preserveAspectRatio="xMidYMid meet" viewBox="0 0 60 60"> <circle cx="30" cy="30" r="30" /> <text x="30" y="30" text-anchor="middle" font-size="0.45em" fill="white"> <tspan dy="0.2em">Click to reorder</tspan> </text> </symbol> </defs> <use order="1" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#sym-cr" x="0" y="0" width="60" height="60" style="fill: #ff9700; z-index: 1;"></use> <use order="4" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#sym-cr" x="50" y="20" width="50" height="50" style="fill: #0D47A1; z-index: 4;"></use> <use order="5" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#sym-cr" x="15" y="30" width="50" height="40" style="fill: #9E9E9E; z-index: 5;"></use> <use order="3" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#sym-cr" x="25" y="30" width="80" height="80" style="fill: #D1E163; z-index: 3;"></use> <use order="2" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#sym-cr" x="30" y="0" width="50" height="70" style="fill: #00BCD4; z-index: 2;"></use> <use order="0" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#sym-cr" x="5" y="5" width="100" height="100" style="fill: #E91E63; z-index: 0;"></use> </svg>

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

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

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

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

如前所述,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: