Sudarshan Sudarshan - 21 days ago 8
Javascript Question

how to drag svg group using d3js drag behavior

i am using D3js drag. single element is get dragged perfectly fine. but i want to drag a group of elements.How it can be done . here is Js Fiddle link. i want to drag both rect and text simultaneously.here i tried but no luck. help me, i know i am missing something simple.

<>

Answer

First off, the <g> element doesn't care about xand y attributes (as in: they're just ignored). You can use transform="translate(x,y)" instead.

Second, you will need to check that the element you get in the dragmove handler actually is the <g> element, and not a child element of it. This is because <g> elements have no actual hit area themselves. Their children do though, and the mouse events first go to the children then bubble up to the parent. You can inspect evt.target and evt.currentTarget to see this in action. target is the element that was hit originally, currentTarget is the event target that is currently handling the event (e.g the <g> element if the event bubbled up).

Comments