jQuery UI Draggable

jQuery UI's .draggable() interaction turns any element into something a user can pick up and move around the page with the mouse or touch. It's the foundation used by droppable, sortable and many custom drag interfaces.

1) Basic draggable


$('#box').draggable();

2) Constrain to an axis


$('#box').draggable({
  axis: 'x' // or 'y' - lock movement to one direction
});

3) Containment


$('#box').draggable({
  containment: '#parent' // also accepts 'window', 'document', or [x1,y1,x2,y2]
});

4) Drag handle


$('#box').draggable({
  handle: '.handle' // only this child starts the drag
});

5) Snapping


$('#box').draggable({
  snap: true,
  snapTolerance: 15 // pixels away before it snaps to nearby elements/edges
});

6) Drag events: start / drag / stop


$('#box').draggable({
  start: function(event, ui){ // fires once, when dragging begins },
  drag: function(event, ui){ // fires repeatedly, ui.position gives {top, left} },
  stop: function(event, ui){ // fires once, when the mouse is released }
});

Live Demo

Drag the green box below by its darker top strip (the handle option). It's constrained inside the dashed container (containment), and the status line updates on drag/stop events.

Drag by top bar
Status: idle
Tip: Combine axis, containment and handle to build things like custom sliders, resizable split panes, or a card you can only drag horizontally inside its row.