[英]d3 mouse events not working
我无法在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>
解决。 在同一视图中有一个可拖动组件,将其删除后,上述问题就消失了。 让我想到的是,d3和jQuery的鼠标事件之间可能会发生冲突。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.