我们如何检测左键单击路径

时间:2016-07-16 14:29:21

标签: javascript d3.js

是否可以检测路径/链接上的左键单击。我想知道如何在mousedown中检测到它。我看到这个链接how-to-distinguish-between-left-and-right-mouse-click-with-jquery但可能是可能的解决方案。

正如您在代码中看到的那样。我将true应用于上下文菜单中的变量isLeftClick,这样当我点击mousedown时,isLeftClick的值为false。但问题是,mousedown将首先出现。

isLeftClick = false;

path.enter().append('svg:path')
.attr('class', 'link')
.classed('selected', function(d) { return d === selected_link; 
})
.on('mousedown', function(d) {
  // detect if it is right or left click
  if(isLeftClick == true){ 
     //if left click do something
    isDraggingLink = true;
  } 
  restart();
}).on('contextmenu', function(d){
    isLeftClick = true;
    // Open a context menu for link/path
    console.log("link contextmenu");
});

1 个答案:

答案 0 :(得分:1)

在'mousedown'回调中你可以访问d3.event变量。它包含对DOM事件的引用,该事件包含有关触发回调的事件的完整信息。

请参阅https://github.com/d3/d3-3.x-api-reference/blob/master/Selections.md#d3_event

https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent

这应该有效:

var leftButtonPressed = (d3.event.button === 0);