我想编辑dragmove函数来接受参数.这是原始代码:
var drag = d3.behavior.drag()
.on("drag", dragmove)
.on("dragend", function(d){console.log("dragend"); });
focus.selectAll("circle")
.data([coordinatesForecastCurrentMonth])
.enter()
.append("circle")
.attr("cx", function(d) {
return x(d[0]);})
.attr("cy", function(d) {
return y(d[1]);})
.attr("r", function(d) {
return 5;})
.attr("clip-path", "url(#clip)")
.call(drag);
function dragmove(d) {
d3.select(this)
.attr("cy", d3.event.y);}
Run Code Online (Sandbox Code Playgroud)
当我将其更改为以下内容时:
.on("drag", dragmove(1,2))
function dragmove(arg1,arg2) {
d3.select(this)
.attr("cy", d3.event.y);
console.log(arg1);
console.log(arg2);}
Run Code Online (Sandbox Code Playgroud)
我收到此错误:

我不明白为什么无法找到事件对象,有人可以帮忙吗?
您在更改中调用该函数而不是传递对它的引用.也就是说,您在执行代码时调用它,而不是在拖动时调用它.因此,没有d3.event.使它成为这样的函数引用.
.on("drag", function() { dragmove(1,2); });
Run Code Online (Sandbox Code Playgroud)
其余代码可以保持不变.
| 归档时间: |
|
| 查看次数: |
4594 次 |
| 最近记录: |