Interactive demos of hcg-draggable, a lightweight zero-dependency JavaScript
library for draggable elements. Try axis lock, grid snap, containment, drag handles, and revert.
Full documentation
Add data-hcg-draggable and the element is draggable on page load.
<div data-hcg-draggable>drag me</div>
Set data-hcg-axis="x" to lock movement to one axis.
<div data-hcg-draggable data-hcg-axis="x">x only</div>
hcgDraggable("#slider", { axis: "x" });
Movement snaps to a fixed pixel step.
<div data-hcg-draggable data-hcg-grid="40">grid 40</div>
hcgDraggable("#box", { grid: [40, 40] });
Use containment: "parent" to keep the element inside its container.
<div class="area">
<div data-hcg-draggable data-hcg-containment="parent">contained</div>
</div>
hcgDraggable("#box", { containment: "parent" });
Only the title bar starts a drag; the body stays interactive.
hcgDraggable("#card", {
handle: ".bar",
onEnd: function (e) { console.log("dropped at", e.x, e.y); }
});
A 5px threshold lets the button click normally. cancel blocks drag starts on the button.
Drag me, or click the button.
hcgDraggable("#clickable", {
dragThreshold: 5,
cancel: "button"
});
Overlapping boxes rise above the others when grabbed.
hcgDraggable(".box", { bringToFront: true });
Change axis or disable dragging on a live instance.
var d = hcgDraggable("#runtime");
d.setOption("axis", "x");
d.setOption("axis", null);
d.setOption("disabled", true);
Contained elements are re-clamped when the window is resized.
<div id="resize-area">
<div data-hcg-draggable data-hcg-containment="#resize-area">contained</div>
</div>
var d = hcgDraggable("#box", { containment: "#resize-area" });
d.contain();
With revert: true, the element animates back when released.
hcgDraggable("#revert-box", {
revert: true
});