我有以下代码:
我有两个小圆圈出现在大圆圈的鼠标悬停上。我面临的问题是 - 当我将鼠标移动到较小的圆圈时,它将消失,从堆栈溢出中发现添加pointer-events:none
将停止此操作。但我需要在这些圈子上绑定点击事件。这个问题有解决方法吗?
d3.selectAll(".node-hover-button").attr("opacity", 0);
d3.select("circle").on("mouseover", function() {
d3.select(this.parentNode).selectAll(".node-hover-button").attr("opacity", 1);
}).on("mouseout", function() {
d3.select(this.parentNode).selectAll(".node-hover-button").attr("opacity", 0);
});
//attach a click event on .node-hover-button.

<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.7/d3.min.js"></script>
<svg>
<g transform="translate(100,50)">
<rect x="50" y="-30" width="50" height="60" id="yesDecision" class="hoverNodeundefined" style="fill: rgb(51, 110, 123);"></rect>
<text x="80" y="0" class="id ">Yes</text>
<circle class="node fixed" r="58" style="fill: rgb(30, 139, 195); stroke: rgb(21, 97, 136);" transform="scale(1.0)"></circle>
<text x="0" y="20" class="id">Segment</text>
<rect class="edit-event node-hover-button" x="-20" y="-70" height="29" width="29" rx="15" ry="15"></rect>
<rect class="delete-event node-hover-button" x="-54" y="-54" height="29" width="29" rx="15" ry="15"></rect>
</g>
</svg>
&#13;
答案 0 :(得分:1)
解决此问题的两种可能方法。
<g>
元素中对较小的圆圈和大圆圈进行分组。方法1:
d3.selectAll(".node-hover-button").attr("opacity", 0);
d3.select("circle").on("mouseover", function() {
d3.select(this.parentNode).selectAll(".node-hover-button").attr("opacity", 1);
}).on("mouseout", function() {
var sCircle1 = d3.selectAll(".node-hover-button")[0][0];
var sCircle2 = d3.selectAll(".node-hover-button")[0][1];
if (d3.event.toElement != sCircle1 && d3.event.toElement != sCircle2) {
d3.select(this.parentNode).selectAll(".node-hover-button").attr("opacity", 0);
}
});
//attach a click event on .node-hover-button.
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.7/d3.min.js"></script>
<svg>
<g transform="translate(100,50)">
<rect x="50" y="-30" width="50" height="60" id="yesDecision" class="hoverNodeundefined" style="fill: rgb(51, 110, 123);"></rect>
<text x="80" y="0" class="id ">Yes</text>
<circle class="node fixed" r="58" style="fill: rgb(30, 139, 195); stroke: rgb(21, 97, 136);" transform="scale(1.0)"></circle>
<text x="0" y="20" class="id">Segment</text>
<rect class="edit-event node-hover-button" x="-20" y="-70" height="29" width="29" rx="15" ry="15"></rect>
<rect class="delete-event node-hover-button" x="-54" y="-54" height="29" width="29" rx="15" ry="15"></rect>
</g>
</svg>
&#13;
方法2:
d3.selectAll(".node-hover-button").attr("opacity", 0);
d3.select(".nodes").on("mouseover", function() {
d3.select(this.parentNode).selectAll(".node-hover-button").attr("opacity", 1);
}).on("mouseout", function() {
d3.select(this.parentNode).selectAll(".node-hover-button").attr("opacity", 0);
});
//attach a click event on .node-hover-button.
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.7/d3.min.js"></script>
<svg>
<g transform="translate(100,50)">
<rect x="50" y="-30" width="50" height="60" id="yesDecision" class="hoverNodeundefined" style="fill: rgb(51, 110, 123);"></rect>
<g class="nodes">
<text x="80" y="0" class="id ">Yes</text>
<circle class="node fixed" r="58" style="fill: rgb(30, 139, 195); stroke: rgb(21, 97, 136);" transform="scale(1.0)"></circle>
<text x="0" y="20" class="id">Segment</text>
<rect class="edit-event node-hover-button" x="-20" y="-70" height="29" width="29" rx="15" ry="15"></rect>
<rect class="delete-event node-hover-button" x="-54" y="-54" height="29" width="29" rx="15" ry="15"></rect>
</g>
</g>
</svg>
&#13;