Draggable class enables you to make DOM elements or JavaScript objects draggable with advanced features like momentum scrolling, container constraints, snap points, and customizable physics.
Constructor
Element to make draggable. Can be:
- CSS selector string (e.g.,
'.draggable') - DOM element
- JavaScript object with
x,y,width,heightproperties
Configuration object for draggable behavior.
Configuration Parameters
Axis Control
Enable or configure horizontal dragging:
true: Enable with defaultsfalse: Disable horizontal dragging- Object: Configure axis behavior
Enable or configure vertical dragging:
true: Enable with defaultsfalse: Disable vertical dragging- Object: Configure axis behavior
Axis Configuration Object
Property to map the x-axis to (e.g.,
'left', 'x', 'scrollLeft').Transform the value before applying.
How dragging composes with other animations.
Snap points specific to this axis.
Container & Boundaries
Constrain dragging within boundaries:
- CSS selector or element: Use element bounds
[top, right, bottom, left]: Custom boundaries in pixels- Function: Dynamically return container
Padding inside the container boundaries:
- Single number: Same padding on all sides
[top, right, bottom, left]: Individual side padding
Friction when dragging outside container (0-1). Higher values = more friction.
Friction when releasing outside container (0-1).
Interaction
Element that triggers the drag. If not specified, uses the target element.
Cursor behavior:
false: No cursor changetrue: Use default cursors- Object: Custom cursor styles
Cursor Configuration
Cursor when hovering over trigger element.
Cursor while dragging.
Drag Behavior
Speed multiplier for drag movement.
Minimum distance in pixels before drag starts:
- Single number: Same for mouse and touch
- Object: Different values for
mouseandtouch
Snap Points
Global snap points for both axes:
- Number: Snap to multiples of this value
- Array: Snap to specific values
- Function: Dynamically calculate snap points
Momentum & Physics
Minimum velocity for momentum scrolling.
Maximum velocity cap for momentum scrolling.
Multiplier for velocity calculations.
Release Animation
Easing function for release animation. Can be spring configuration or standard easing.
Spring mass for release animation (when using spring physics).
Spring stiffness for release animation.
Spring damping for release animation.
Auto-Scroll
Speed of auto-scrolling when dragging near container edges.
Distance from edge (in pixels) that triggers auto-scroll.
Modifiers
Global modifier function for both x and y values.
Event Callbacks
Called when dragging starts (on mousedown/touchstart).
Called continuously while dragging.
Called when drag is released (on mouseup/touchend).
Called on every position update (during drag and release animation).
Called when the element comes to rest after release.
Called when element snaps to a snap point.
Called when container or target is resized.
Called after resize calculations complete.
Instance Properties
Position Properties
Current x position. Can be read or set directly.
Current y position. Can be read or set directly.
Normalized x position (0-1) within container bounds.
Normalized y position (0-1) within container bounds.
Change in x position since last frame.
Change in y position since last frame.
Current velocity magnitude.
Current drag angle in radians.
State Properties
Whether the element is currently grabbed.
Whether dragging has started (moved beyond threshold).
Whether the element was just released.
Whether the draggable is currently enabled.
Target Elements
The target DOM element being dragged.
The element that triggers the drag interaction.
The container element (if specified).
Internal Animatable instance used for animations.