Skip to main content
100%

Click vs. Drag

✓ Published0🌍 Public
Mmbostock
Last edited Jul 5, 2019
Created on Mar 1, 2016

Twenty colored circles start at random positions. Clicking a circle briefly inflates it to black and back, while dragging moves it to a new location. Using `d3.behavior.drag` with d3.v3, the example distinguishes the two gestures by checking `d3.event.defaultPrevented` in the click handler. The SVG rendering animates transitions for the inflation effect and reorders dragged elements to the front.

AI-generated description

This example demonstrates how to disambiguate a click from a drag gesture using d3.event.defaultPrevented. Drag any circle to change its position, or click it to briefly inflate.

Note that you must be careful not to re-append the element on dragstart (mousedown), as this may prevent the browser from dispatching a click event! Instead, defer the move-to-front until the first drag event.

gpl-3.0 Licensed

Similar vizzes