简体   繁体   English

如何以编程方式触发D3拖动事件?

[英]How to programmatically trigger a D3 drag event?

So i have some data binded with drag event listeners : 所以我有一些与拖动事件监听器绑定的数据:

myNodes.enter()
    .append("svg:g")
    .call(d3.behavior.drag()
        .on("drag", function() { 
            console.log(d3.event.dx, d3.event.dy);
        })
    );

Now I want to call this onDrag function on a certain node programmatically. 现在我想以编程方式在某个节点上调用此onDrag函数。 I do know the same is possible with standard events by doing 我知道标准事件可以做到这一点

aNode.on("click")() // works
aNode.on("drag")()  // doesn't work

Is there any way to do so ? 有没有办法这样做? Thanks. 谢谢。

Save the callback (the one you pass into the drag handler) in a variable and then call this variable in your other context. 将回调(传递给拖动处理程序的回调)保存在变量中,然后在其他上下文中调用此变量。

var dragCallback = function(){
  console.log(d3.event.dx, d3.event.dy);
};
var dragBehavior = d3.behavior.drag()
                     .on("drag", dragCallback);
myNodes.enter()
  .append("g")
  .call(dragBehavior);
  //Call drag method programmatically
  dragCallback()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM