我试图在其他圆圈上拖一个圆圈来连接两条路径.问题是当我将一个圆圈(c2)拖到另一个圆圈(c1)上时,如果在圆圈c2之前创建圆圈c1,则不会触发c1鼠标悬停事件.
这里的js小提琴链接
var closedRoad = true; var width = 960, height = 500; var points = d3.range(1, 5).map(function(i) { return [i * width / 5, 50 + Math.random() * (height - 100)]; }); var points1 = d3.range(1, 5).map(function(i) { return [i * width / 5, 50 + Math.random() * (height - 100)]; }); var points2 = d3.range(1, 5).map(function(i) { return [i * width / 5, 50 + Math.random() * (height - 100)]; }); var count = 0; var ways = []; var currentWay = null; var selected = null; var line = d3.svg.line(); var svg = d3.select("body").append("svg") .attr("width", width) .attr("height", height); var rect = svg.append("rect") .attr("width", width) .attr("height", height); ways.forEach(function(way, i) { svg.append("path") .datum(way) .attr("id", "p" + way.id) .attr("class", "line") .call(redraw); }); d3.select(window) .on("keydown", keydown) //.on("mousemove", mousemove) //.on("mouseup", mouseup) .on("mousedown", mousedown) .on("dblclick", dblclick); function redraw(way) { way.attr("d", function(d) { return line(d.pts); }); var circle = svg.selectAll(".way" + way.data()[0].id) .data(way.data()[0].pts, function(d) { return d; }); circle.enter().append("circle") .attr("class", "way" + way.data()[0].id) .attr("r", 1e-6) .on("mousedown", function(d) { if (closedRoad) { currentWay = way.data()[0]; selected = d; if (d3.event) { d3.event.preventDefault(); d3.event.stopPropagation(); } } }) .on("mouseover", function() { d3.select(d3.event.target).classed("highlight", true); }) .on("mouseout", function() { d3.select(d3.event.target).classed("highlight", false); }) .transition() .duration(750) .ease("elastic") .attr("r", 6.5); circle .attr("cx", function(d) { return d[0]; }) .attr("cy", function(d) { return d[1]; }); //// var drag = d3.behavior.drag(); drag.on("dragstart", function(d) { console.log('START'); }) .on("drag", function(d) { console.log('MOVE'); var m = d3.mouse(svg.node()); d[0] = Math.max(0, Math.min(width, m[0])); d[1] = Math.max(0, Math.min(height, m[1])); //redraw(way); redrawAll(); }) .on("dragend", function(d) { console.log('END'); }); circle.call(drag); //// circle.exit().remove(); } function dblclick() { currentWay.pts.pop(); //redraw(svg.select("#p" + currentWay.id)); redrawAll(); closedRoad = true; } function mousedown() { if (closedRoad) { currentWay = { id: ++count, pts: [] }; ways.push(currentWay); svg.append("path") .datum(currentWay) .attr("id", "p" + currentWay.id) .attr("class", "line") .on("mouseover", function() { d3.select(d3.event.target).classed("highlight", true); }) .on("mouseout", function() { d3.select(d3.event.target).classed("highlight", false); }) .call(redraw); closedRoad = false; } currentWay.pts.push(selected = d3.mouse(svg.node())); //redraw(svg.select("#p" + currentWay.id)); redrawAll(); } function redrawAll() { ways.forEach(function(way, i) { redraw(svg.select("#p" + way.id)); }); } function mousemove() { } function keydown() { if (!selected) return; switch (d3.event.keyCode) { case 8: // backspace case 46: { // delete var i = currentWay.pts.indexOf(selected); currentWay.pts.splice(i, 1); selected = currentWay.pts.length ? currentWay.pts[i > 0 ? i - 1 : 0] : null; redraw(svg.select("#p" + currentWay.id)); break; } } }
你能帮助我吗?
谢谢!前!
问题很简单,"鼠标悬停"事件仅在两个元素一个在另一个上面绘制时才会在最顶层元素上触发.无论您是否正在处理最顶层元素的鼠标事件,都是如此.改变这种行为需要大量的解决方案,其中没有一个是理想的.
一些可能的解决方
在拖动功能中,反复检查此时是否有另一个圆圈.
您可以使用该SVGSVGElement.getIntersectionList()
方法查找给定矩形中的所有元素.请注意,此方法在SVG 节点上调用,而不是在d3选择上调用.
拖动节点时,通过pointer-events:none;
在其上设置样式使其对鼠标事件"透明" .
当然,这也会使它对拖动事件透明,因此您必须将拖动事件添加到容器而不是节点,然后找出在'dragstart'事件中拖动哪个圆,更改其指针 - 事件样式,并将圆选择存储在您的drag
函数可访问的变量中(将其移动与当前代码相同)和dragend
函数(将重新设置其指针事件样式).你还需要一个背景矩形添加到<svg>
或<g>
分配拖拽行为元素,使之鼠标事件,即使鼠标仅在"透明的"圈做出回应.
拖动节点时,将其移动到绘制顺序的底部,以便将所有其他节点绘制为顶部.
由于SVG没有像HTML那样的"z-index"属性,因此实现此目的的唯一方法是使用selection.order()
或简单的Javascript 实际重新排序DOM insertBefore()
.
或者,接受这只是一个小的美学缺陷,并且不会改变代码的功能,因此不值得上述方法的麻烦和性能影响.
如果您不喜欢某些圆圈在拖动时会改变颜色这一事实,您可以在拖动期间在svg上设置"拖动"类,并且让"突出显示"类的css仅在下面进行可见的更改它不是'svg.dragging'的孩子.