【问题标题】:Stop dragging on a force layout停止拖动强制布局
【发布时间】:2015-06-17 14:36:43
【问题描述】:

我在 d3 中有一个强制布局,并且我的节点中有以下项目的代码...

drag = force.drag()
        .on("dragstart", dragstart);
...
circle = node.append("circle")
          .attr("r", 36)
          .on("dblclick", dblclick)
          .call(drag);
...
node.on("mousedown", function(d){
          if(d == lastNode){
              circle.on(".drag", null);
              drag_line
                .attr("class", "link")
                .attr("x1", d.x)
                .attr("y1", d.y)
                .attr("x2", d.x)
                .attr("y2", d.y);
          }
          else{
              lastNode = mousedown_node = d;
          }
          svg.call(disabledZoom);

      })

问题是,即使我将拖动设置为空,圆圈仍然会拖动。谁能看到我错过了什么?

【问题讨论】:

  • “将拖动设置为空”是什么意思?更改变量不会改变行为,您需要为此重置事件处理函数。
  • 是的,这就是我所要求的 :) 要消除拖动行为,请参阅 stackoverflow.com/questions/13136355/…

标签: javascript d3.js


【解决方案1】:

我认为这有点令人困惑,但正如您在 Wiki 中看到的那样,禁用拖动行为的方式是这样的......

node.on(".drag", null);
//not sure if you have any translations or setting of attributes in 
//your zoom behaviour so maybe this will help also...
node.on(".zoom", null);

【讨论】:

  • 这对我不起作用,这是我原来的样子
  • @Jackie 如果您发布一个展示该行为的最低工作示例,那么我相信我们可以解决您的问题。
  • @Jackie 还添加了适当取消缩放行为,以防造成拖拽。
【解决方案2】:

或者当您不想再拖动时,您只需将布尔值 var 设置为 false。因此,您以后也可以轻松地改回工作拖动。所以在你的拖动功能中你有:

function drag(){
    if (bool){
        //do the dragging stuff
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-12
    • 2013-01-02
    • 2014-07-12
    • 1970-01-01
    • 2019-05-14
    • 2014-08-29
    • 2014-01-15
    • 1970-01-01
    相关资源
    最近更新 更多