【问题标题】:Issue with mouse over and mouse out d3.js鼠标悬停和鼠标移出 d3.js 的问题
【发布时间】:2017-01-18 12:20:40
【问题描述】:

我有以下代码:

我有两个小圆圈出现在大圆圈的鼠标悬停上。我面临的问题是-当我将鼠标移到较小的圆圈上时,它会消失,从堆栈溢出中发现添加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>

【问题讨论】:

  • 只需在小圆圈上使用与大圆圈相同的鼠标悬停事件。即在鼠标悬停时显示(保持打开)2 个小圆圈

标签: javascript d3.js


【解决方案1】:

解决此问题的两种可能方法。

  1. 使用 d3 事件的 toElement 属性。
  2. 在单独的&lt;g&gt; 元素中将小圆圈和大圆圈分组。

方法一:

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.
<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>

方法二:

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.
<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>

【讨论】:

    猜你喜欢
    • 2011-05-26
    • 2011-09-15
    • 1970-01-01
    • 1970-01-01
    • 2018-11-07
    • 1970-01-01
    • 2015-04-22
    • 1970-01-01
    • 2012-05-23
    相关资源
    最近更新 更多