本文介绍了D3.js:从选择中删除force.drag的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个(很简单)的问题:如何解除调用force.drag由D3.js做的选择?让我们说,我创建了一组元素,并在其上调用call,给它一个力导向布局的拖拽回调。看起来像这样:

I have a (rather simple) question: How to "un-call" force.drag on a selection made by D3.js? Let's say I created a set of elements and called "call" on it, giving it the drag-callback of a force-directed layout. That looked like this:

    d3.selectAll('rect').call(force.drag);

现在,以后可以从某些节点中删除该行为。我的方法包括使用

Now it shall be possible to remove that behavior from some of the nodes later on. My approaches included resetting various listeners, such as 'click', 'drag' etc. using

    d3.select('rect#no-drag').on('click', null);

没有人工作。是否有人知道如何删除回调?

None of them worked. Does anybody know how to remove the callback?

推荐答案

拖动事件由具有名为 drag 的命名空间的 mousedown 事件启动。请参阅:

You are close. The drag event is initiated by a mousedown event with a namespace called drag. See: https://github.com/mbostock/d3/blob/master/src/behavior/drag.js#L5

因此,要移除此操作,您可以执行以下操作:

So, to remove this you could do:

d3.select('rect#no-drag').on('mousedown.drag', null);

这篇关于D3.js:从选择中删除force.drag的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-10 22:55