【问题标题】:Dragging on force layout prevents other mouseup listeners拖动强制布局可防止其他 mouseup 侦听器
【发布时间】:2013-01-02 03:38:20
【问题描述】:

我想在 d3.js 强制布局中启用拖动。当拖动圆圈并释放鼠标按钮时,我想通过回调调用特定的函数,如下所示:

this.force = d3.layout.force()
    .nodes(this.nodes)
    .size([this.width, this.height]);

// enable dragging
this.circle
    .call(this.force.drag)
    .on("dragend", function() {
        console.log("You should see this, when releasing a circle.");
    })
    .on("mouseup.drag",function(d,i) {
        console.log("Or see this.");
    });

不幸的是,force.drag 处理程序从未完全触发/消耗该事件。 那么如何在拖动结束时在 d3 强制布局中执行给定的回调函数?

【问题讨论】:

    标签: d3.js mouseevent drag force-layout mouseup


    【解决方案1】:

    您没有在此处调用this.force.drag 上的"dragend" 事件。 这也取决于您如何定义this.force.drag

    这应该适合你

    myCustomDrag = d3.behavior.drag()
        .on("dragstart", function(d,i){
            //do something when drag has just started
        })
        .on("drag", function(d,i){
            //do something while dragging
        })
        .on("dragend", function(d,i){
            //do something just after drag has ended
        });
    

    在上面的代码中,只需在您希望出现此拖动行为的元素(此处圈出)上使用call(myCustomDrag)

    【讨论】:

    • this.force.drag 来自 d3 库,它不是用户定义的。 This answer 接近了,但我无法让它工作,这就是我一直在寻找的原因。
    • 是的,它来自 d3 库,但您始终可以覆盖它。您所指的答案只是编写上述代码的另一种方式。
    • 我最终或多或少地做了你在这里所做的事情。 +1
    • 去掉每个ons后面的分号——它们会阻止方法链接。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多