観察された行動
私は d3.js を使用しています。dragイベントに基づいて一部のデータを更新し、イベント後にすべてを再描画したい状況にありdragendます。ドラッグ可能なアイテムにもいくつかのclick動作があります。
ドラッグ可能なアイテムは、x 軸に沿ってのみ移動できます。アイテムがドラッグされ、カーソルが のドラッグ可能なアイテムの真上にある場合、イベントを発生させるdragend/mouseupには、再描画後にアイテムを 2 回クリックする必要があります。clickアイテムがドラッグされたがdragend/mouseup、アイテムの真上にない場合、clickイベントは再描画後に (最初の試行で) 期待どおりに発生します。
望ましい動作
clickカーソルがどこにあるかに関係なく、ドラッグ後の最初のクリックで常にイベントが発生するようにします。
clickドラッグ可能なアイテムのイベントをイベントに置き換えるとmouseup、すべてが期待どおりに機能しますclickが、実際に処理したいイベントです。
デモンストレーション
自己完結型の例を次に示します: http://jsfiddle.net/RRCyq/2/
そして、ここに関連する JavaScript コードがあります。
var data, click_count,did_drag;
// this is the data I'd like to render
data = [
{x : 100, y : 150},
{x : 200, y : 250}
];
// these are some elements I'm using for debugging
click_count = d3.select('#click-count');
did_drag = d3.select('#did-drag');
function draw() {
var drag_behavior,dragged = false;
// clear all circles from the svg element
d3.select('#test').selectAll('circle')
.remove();
drag_behavior = d3.behavior.drag()
.origin(Object)
.on("drag", function(d) {
// indicate that dragging has occurred
dragged = true;
// update the data
d.x = d3.event.x;
// update the display
d3.select(this).attr('cx',d.x);
}).on('dragend',function() {
// data has been updated. redraw.
if(dragged) { draw(); }
});
d3.select('#test').selectAll('circle')
.data(data)
.enter()
.append('circle')
.attr('cx',function(d) { return d.x; })
.attr('cy',function(d) { return d.y; })
.attr('r',20)
.on('click',function() {
did_drag.text(dragged.toString());
if(!dragged) {
// increment the click counter
click_count.text(parseInt(click_count.text()) + 1);
}
}).call(drag_behavior);
}
draw();