在 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)
解决办法是添加".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
注意:如果较大的圆圈将其填充属性设置为“无”,则该示例也可以按预期工作,而无需将“指针事件”设置为无。