D3中的鼠标位置

9 浏览
0 Comments

D3中的鼠标位置

我想使用以下代码通过D3来获取鼠标位置:

var x = 0;
svg.on('mousemove', function () {
   x = d3.mouse(this)[0];         
});

但是x始终等于0。通过使用console.log(),我可以看到x的值在function()内部发生了变化,但在外部x恢复了初始值0

我该如何保存x的值并在应用程序中稍后使用它?

0