D3.js 鼠标悬停无法触发重叠对象

geo*_*ory 5 d3.js

在 D3.js 中,似乎在其他对象之前绘制的对象然后隐藏它们对鼠标悬停侦听器变得不可见。有解决方法吗?

请参阅此工作示例

<!DOCTYPE html>
<meta charset="utf-8">
<head>
    <script type="text/javascript" src="scripts/d3.v3.js"></script>
</head>
<body>
    <div id="viz"></div>
    <script type="text/javascript">

    d3.select("body").style("background-color", "black");

    var sampleSVG = d3.select("#viz")
        .append("svg")
        .attr("width", 400)
        .attr("height", 200);

    sampleSVG.append("circle")
        .style("fill", "grey")
        .style("stroke-width", 2)
        .attr("r", 60)
        .attr("cx", 150)
        .attr("cy", 100)
        .on("mouseover", function(){d3.select(this).style("fill", "red");})
        .on("mouseout", function(){d3.select(this).style("fill", "grey");});

    sampleSVG.append("circle")
        .style("stroke", "yellow")
        .style("opacity", 0.5)
        .style("stroke-width", 2)
        .attr("r", 100)
        .attr("cx", 250)
        .attr("cy", 100)

    </script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

Rob*_*inL 5

解决办法是添加".style("pointer-events", "none");" 对于最后一个圆圈:

sampleSVG.append("circle")
    .style("stroke", "yellow")
    .style("opacity", 0.5)
    .style("stroke-width", 2)
    .attr("r", 100)
    .attr("cx", 250)
    .attr("cy", 100)
    .style("pointer-events", "none");
Run Code Online (Sandbox Code Playgroud)

这是一个工作示例http://tributary.io/inlet/5215694

注意:如果较大的圆圈将其填充属性设置为“无”,则该示例也可以按预期工作,而无需将“指针事件”设置为无。

  • 这确实解决了我眼前的问题。但 10 的好处是如何同时选择两个对象?我想一个解决方法可能是暂时调用 `.style("pointer-events", "none")` 并监听 `each()` 以便在完成后重新激活,但希望有一个更优雅的解决方案。 (2认同)