代码之家  ›  专栏  ›  技术社区  ›  manymanymore

未捕获错误:未知类型:d3.js v5.4.0中的dragend

  •  0
  • manymanymore  · 技术社区  · 6 年前

    我用的是 d3.js v5.4.0 .

    以下是我得到的错误: Uncaught Error: unknown type: dragend .

    这是因为我正在尝试执行以下操作:

                d3.drag()
                .subject(function(d){
                    return {x: d.x, y: d.y};
                })
                .on("drag", function(args){
                    thisGraph.state.justDragged = true;
                    thisGraph.dragmove.call(thisGraph, args);
                })
                .on("dragend", function() {
                    // todo check if edge-mode is selected
                });
    

    dragend 现在似乎被否决了。

    我试着找出发行说明,在新版本中描述了替代它的方法。 here 但没能做到。

    请帮我解决这个问题。

    1 回复  |  直到 6 年前
        1
  •  1
  •   Andrew Reid    6 年前

    您可以通过拖动收听的三个事件当前是(v4&5)。v3和之前的版本不同):

    开始-新指针激活后(在鼠标向下或触摸屏上)。 拖动-活动指针移动后(鼠标移动或触摸移动)。 结束-活动指针变为非活动状态后(在mouseup、touchend或touchcancel上)。( docs )

    所以,你只需要把dragend换成end

    var svg = d3.select("body")
      .append("svg")
      .attr("width",500)
      .attr("height",300);
      
    var circle = svg.append("circle")
      .attr("cx",100)
      .attr("cy",100)
      .attr("r",20)
      .attr("fill","steelblue")
      .call(d3.drag().on("start", function() {
          d3.select(this).attr("fill", "orange")
        })
        .on("drag", function() {
          d3.select(this).attr("cx",d3.event.x)
            .attr("cy", d3.event.y )
        })
        .on("end", function() {
          d3.select(this).attr("fill","steelblue");
        })
      )
    <script src="https://d3js.org/d3.v5.js"></script>