2013-01-18 25 views
3

我想要在d3.js強制佈局中啓用拖動。當拖動一個圓圈,然後鬆開鼠標按鈕,我想通過回調來調用特定的功能,如:在強制佈局上拖動可以防止其他mouseup偵聽器

this.force = d3.layout.force() 
    .nodes(this.nodes) 
    .size([this.width, this.height]); 

// enable dragging 
this.circle 
    .call(this.force.drag) 
    .on("dragend", function() { 
     console.log("You should see this, when releasing a circle."); 
    }) 
    .on("mouseup.drag",function(d,i) { 
     console.log("Or see this."); 
    }); 

不幸的是,事件從來沒有發射/由force.drag處理器完全消耗。 那麼如何在拖動結束時在d3強制佈局中執行給定的回調函數?

回答

3

這裏您沒有撥打this.force.drag上的"dragend"事件。 這也取決於你如何定義this.force.drag

這應該爲你工作

myCustomDrag = d3.behavior.drag() 
    .on("dragstart", function(d,i){ 
     //do something when drag has just started 
    }) 
    .on("drag", function(d,i){ 
     //do something while dragging 
    }) 
    .on("dragend", function(d,i){ 
     //do something just after drag has ended 
    }); 

在上面的代碼,只是一個元素要在其上受此拖累行爲存在(圓圈這裏)上使用call(myCustomDrag)

+0

'this.force.drag'來自d3庫,它不是用戶自定義的。 [這個答案](http://stackoverflow.com/a/11867374/295686)會接近,但我無法得到它的工作,這就是爲什麼我一直在尋找。 – mlhDev

+0

是的,它來自d3庫,但您始終可以覆蓋它。 您提到的答案只是編寫上述代碼的另一種方式。 – arunkjn

+0

我最終做了或多或少的你在這裏。 +1 – mlhDev

相關問題