简体   繁体   English

拖动行为行不通吗?

[英]Drag behavior not working right?

I'm trying to find the problem with the drag behavior setup that I have in my program, because it seems like the drag won't even activate. 我正在尝试查找程序中存在的拖动行为设置的问题,因为似乎拖动甚至无法激活。 I'm using http://jsfiddle.net/da37B/317/ as the reference code for my program. 我正在使用http://jsfiddle.net/da37B/317/作为程序的参考代码。

Here's the relevant code: 以下是相关代码:

 vis.selectAll(".nodes")
        .data(nodes)
        .enter().append("circle")
        .attr("class", "nodes")
        .attr("cx", function (d) {
            return xRange(d.x);
        })
        .attr("cy", function (d) {
            return yRange(d.y);
        })
        .attr("r", "10px")
        .attr("fill", "black")
        .attr("transform", "translate(" + p.x + "," + p.y + ")")
        .call(drag); <------

// Define drag beavior
var drag = d3.behavior.drag()
    .on("drag", dragmove);

function dragmove(d) {
    var x = d3.event.x;
    var y = d3.event.y;
    d3.select(this).attr("transform", "translate(" + x + "," + y + ")");
}

And here's the full code: https://jsfiddle.net/4o5pch1q/1/ 这是完整的代码: https : //jsfiddle.net/4o5pch1q/1/

The reason you don't see any effect is that you have an error in your jsfiddle. 您没有看到任何效果的原因是您的jsfiddle中存在错误。 Please check the console for such obvious things in the future. 以后请检查控制台中是否有明显的事情。

Once the obvious error is fixed (including moving the definition of drag up so that it's defined before it's being used), the only thing that remains is to tell D3 how to get the origin of the element being dragged (otherwise the circle "jumps" on drag): 一旦解决了明显的错误(包括向上drag的定义,以便在使用之前对其进行定义),剩下的唯一事情就是告诉D3如何获取被拖动元素的原点(否则圆圈“跳转”)拖动时):

var drag = d3.behavior.drag()
    .origin(function(d) { return d; })
    .on("drag", dragmove);

Complete demo here . 在此处完成演示。

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

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