简体   繁体   English

d3拖动行为:在拖动名称空间中注册,监听和触发不同类型?

[英]d3 drag behavior: registering, listening, and triggering distinct types in the drag namespace?

Typing d3.behavior.drag() has me confused. 输入d3.behavior.drag()让我感到困惑。 I'd like to specify drag types so that different objects trigger different drag events. 我想指定拖动类型,以便不同的对象触发不同的拖动事件。 I'm used to programming with D3 dispatches/namespaces doing the following: 我习惯使用D3调度/命名空间进行以下操作:

  1. registering them with the D3 dispatch object 向D3分发对象注册它们
  2. then using something like .on('click.typeHere') to listen for the dispatch event 然后使用类似.on('click.typeHere')来监听调度事件
  3. and using dispatch.typeHere(<<data>>) to trigger the event. 并使用dispatch.typeHere(<<data>>)触发事件。

I can't figure out how the normal dispatch procedure applies to drag behavior. 我不知道正常的分派过程如何应用于拖动行为。 For example: 例如:

 var svg = d3.select('svg').append('g') .attr('transform','translate(0,20)'); svg.append('rect') .attr('height', 50) .attr('width', 20) .style('fill', 'steelblue'); var drag = d3.behavior.drag(); drag.on('drag', function() { console.log('drag'); }).on('drag.type', function() { console.log('namespace active'); }).on('drag.type2', function() { console.log('namespace2 active'); }); svg.append('circle') .attr('cx',20) .attr('cy',0) .attr('r',10) .style('fill', 'orange') .call(drag); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.min.js" charset="utf-8"></script> <svg></svg> 

I want the circle to trigger just drag.type when it is dragged and not the other drags that are registered in the drag behavior namespace. 我希望圆圈在drag.type触发drag.type ,而不触发在拖动行为名称空间中注册的其他拖动。 But .call(drag.type) isn't supported. 但是不支持.call(drag.type) What is the right way to go about this? 解决这个问题的正确方法是什么?

I would use different drag behaviours for the different types of objects with the respective drag handlers doing the appropriate thing. 对于不同类型的对象,我将使用不同的拖动行为,并由各自的拖动处理程序执行适当的操作。 This will save you having to mess around with event namespaces: 这将使您不必去弄乱事件名称空间:

var drag1 = d3.behavior.drag()
.on('drag', function() {
  console.log('drag rect');
});

svg.append('rect').call(drag1);

var drag = d3.behavior.drag()
.on('drag', function() {
  console.log('drag circle');
});

svg.append('circle').call(drag);

Complete demo here . 在此处完成演示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM