如何以编程方式触发D3拖动事件? [英] How to programmatically trigger a D3 drag event?

查看:164
本文介绍了如何以编程方式触发D3拖动事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一些数据绑定拖动事件侦听器:

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);
        })
    );

现在我想以编程方式调用某个节点上的onDrag函数。我知道标准事件可以做同样的事情

Now I want to call this onDrag function on a certain node programmatically. I do know the same is possible with standard events by doing

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

有办法吗?感谢。

推荐答案

将变量中的回调(传递到拖动处理程序中的回调)保存在变量中,然后在其他上下文。

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()

这篇关于如何以编程方式触发D3拖动事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆