d3鼠标事件不起作用

时间:2015-08-28 16:19:16

标签: javascript jquery d3.js svg

我无法在mousedown,mouseup和mousemove上触发d3事件。当我将它移动到生产服务器时,代码不起作用(否则它可以使用简单的index.html和只有jquery和d3库工作)。在生产文件夹中,还有其他可拖动元素的库与svg和d3实现没有任何关系,我试图在这里完成。我只是认为他们可能成为与鼠标事件冲突的可能原因:(任何帮助将不胜感激)

这里是我们的代码(在生产环境之外工作正常):我试图在鼠标移动时划一条线:

    var container  = d3.select('svg');

    function draw(selection){
        var xy0, 
            path, 
            keep = false, 
            line = d3.svg.line()
                     .x(function(d){ return d[0]; })
                     .y(function(d){ return d[1]; });
        selection
            .on('mousedown', function(){      

            console.log("100")  

              console.log('THIS', this)
                keep = true;                     
                xy0 = d3.mouse(this);
                console.log('xy0', xy0)
                path = d3.select('svg')
                         .append('path')
                         .attr('d', line([xy0, xy0]))
                         .style({'stroke': 'red', 'stroke-width': '3px'});

                         console.log(path)


            })
            .on('mouseup', function(){ 
                var xUp = d3.mouse(this);

                console.log('xUp', xUp)

                keep = false; 
            })
            .on('mousemove', function(){ 
                if (keep) {
                    Line = line([xy0, d3.mouse(this)
                                .map(function(x){ return x - 1; })]);
                    console.log(Line);
                    path.attr('d', Line);
                }

                var xMove = d3.mouse(this);

                    console.log('x', xMove[0]);
                    console.log('y', xMove[1]);

                console.log('xMove', xMove)
            });
    }

 d3.select('svg').call(draw);

这是HTML

            <image xlink:href="dot.svg" x="7%" y="35%" height="20px" width="20px"></image>
             <image xlink:href="dot.svg" x="36%" y="35%" height="20px" width="20px"></image>
            <image  xlink:href="dot.svg" x="65%" y="35%" height="20px" width="20px"></image>


            <image xlink:href="dot.svg" x="7%" y="70%" height="20px" width="20px"></image>
             <image xlink:href="dot.svg" x="36%" y="70%" height="20px" width="20px"></image>
            <image  xlink:href="dot.svg" x="65%" y="70%" height="20px" width="20px"></image>

         </svg>

1 个答案:

答案 0 :(得分:0)

解决。在同一视图中有一个可拖动的组件,当移除时,上述问题就会消失。这让我想到,d3和jQuery的鼠标事件之间可能会发生冲突。