Web Development
Draggable and Inertia: Dragging, Throwing and Snapping with GSAP
Draggable is the GSAP plugin that makes an element draggable with a mouse or a finger, and InertiaPlugin, its companion, lets the element keep its momentum on release and glide to a stop. Both have been free since 3.13.
A product photo gallery on a phone uses both: it slides under a finger, keeps going a little after release and stops right on a photo, while two buttons do the same without dragging. That is the demo in this post, a carousel of five cards.
This post is part of the GSAP series. Licensing and installation live in the series hub, and matchMedia, context and gsap.utils in post 2, on the core. It includes a live carousel on GSAP 3.15.0. Three script tags are enough:
<script src="https://unpkg.com/gsap@3.15.0/dist/gsap.min.js"></script>
<script src="https://unpkg.com/gsap@3.15.0/dist/Draggable.min.js"></script>
<script src="https://unpkg.com/gsap@3.15.0/dist/InertiaPlugin.min.js"></script>
<script>
gsap.registerPlugin(Draggable, InertiaPlugin);
</script>
In the snippets, .viewport is a box with overflow: hidden, .rail is a flex row of cards inside it, and .chip and .stage are an element and its box.
The basics: Draggable.create()
One line does it (the Draggable docs cover the rest of the options):
Draggable.create(".chip", { type: "x,y", bounds: ".stage" });
type sets which properties the drag changes: "x,y" by default, "x" for a carousel, "y" for a list and "rotation" for a knob. With "left,top", "left" or "top", the element needs position: relative or absolute.
Bounds and edgeResistance
bounds keeps the drag inside an element, a selector or an object such as { minX: 10, maxX: 300 }; an element used as bounds should not be rotated differently from the dragged one. If the selector matches nothing, Draggable.create() throws a TypeError and creates no drag. edgeResistance, from 0 to 1, is the pull past the edge: 1 lets nothing through, and 0.75 makes the element travel "at quarter-speed beyond the border". The demo uses 0.85.
Inertia: letting go with momentum
InertiaPlugin keeps a property's speed and slows it down until it stops. In Draggable, inertia: true with the plugin loaded is all it takes. The tuning knobs:
throwResistance(1000 by default): the friction; the higher, the sooner it stops.minDurationandmaxDuration: the shortest and longest a throw can last.overshootTolerance: how much room it has to overshoot a limit before coming back; 0 allows none.
The docs list 0.2 s, 10 s and 1 as defaults, but those are InertiaPlugin's own: when they are not set, Draggable 3.15.0 uses 0.5 s, 2 s and 1 - edgeResistance + 0.2. On release, the instance already knows where it will land (endX) and whether momentum is still moving it (isThrowing).
Snapping: snap and liveSnap
snap decides where the element lands after release: a function that receives the value where it would stop without snapping, an array whose closest value wins, an object with one rule per property, or points with a radius (it only snaps when it lands near a point). liveSnap applies the rules during the drag. Same idea as ScrollTrigger's snap in post 5, with a pointer. The demo carousel snaps like this:
const viewport = document.querySelector(".viewport");
const rail = document.querySelector(".rail");
const step = 300; // one card's width plus the gap
const minX = Math.min(0, viewport.clientWidth - rail.scrollWidth);
const snapX = (v) => gsap.utils.clamp(minX, 0, Math.round(v / step) * step);
const drag = Draggable.create(rail, {
type: "x",
bounds: { minX: minX, maxX: 0 },
edgeResistance: 0.85,
inertia: true,
snap: { x: snapX }
})[0];
Math.round(v / step) * step rounds the landing spot to the nearest card, and gsap.utils.clamp keeps it on the rail. step has to be the real width of a card plus the gap: when each card and its gap take 250 px, the rail stops halfway through one. The demo measures it with offsetLeft.
Callbacks and update()
onPress fires on press; onDragStart once the pointer moves more than 2 px; onDrag on every move; onDragEnd on every release. onClick only fires below 3 px of movement, and onThrowUpdate and onThrowComplete follow the throw. Inside them, this is the instance: this.x, this.minX and so on.
When another animation moves the element, update() has to be called, since it "Updates the Draggable's x/y properties to reflect the target element's current position". The carousel buttons do that, continuing the snippet above; moving forward means subtracting from x, since the rail slides left:
document.querySelector(".next").addEventListener("click", () => {
gsap.to(rail, {
x: snapX(drag.x - step),
duration: 0.5,
onComplete: () => drag.update()
});
});
Two clicks in a row, before the first tween ends, move a single card: drag.x only updates when the tween completes.
Clicks and touch
A click on an <a>, <input>, <select>, <button> or <textarea> inside the element goes through as usual, but on GSAP 3.15.0 dragging from one of them moves the element too. dragClickables: false keeps them from starting a drag and also covers anything with data-clickable="true"; clickableTest changes the rule. On touch screens, with type: "x", allowNativeTouchScrolling (on by default) lets a vertical swipe keep scrolling the page; the demo also sets touch-action: pan-y on the rail.
InertiaPlugin without Draggable
InertiaPlugin animates any numeric property from a starting velocity, no dragging involved:
const obj = { x: 0, y: 0 };
gsap.to(obj, { inertia: { x: 500, y: -300 } });
gsap.to(".marker", { inertia: { x: { velocity: 800, end: [0, 100, 200] } } });
In the first one, x starts at 500 px per second and y at -300, and both slow down on their own. Each property takes velocity, min and max (limits on where it ends up, not on the speed), resistance and end; an array in end works as "notches": if it would stop at 141, it stops at 100. To read the velocity of something moved by other means, there are InertiaPlugin.track() and getVelocity().
Accessibility: dragging can't be the only way
The WCAG 2.2 accessibility guidelines cover this in two criteria. 2.5.7 (level AA): everything that can be done by dragging must also work with a single tap or click, without dragging, unless dragging is essential. 2.1.1 (level A): everything must work from a keyboard. The Understanding document for 2.5.7 adds that the alternative can't be another swipe. The Draggable docs don't cover the keyboard, so the buttons have to be added separately.
The carousel has "Previous" and "Next" (tap, click, Enter or Space) and an "N of 5" line that screen readers announce whenever it changes (aria-live). It follows the carousel pattern from the ARIA guide (APG): the container is announced as "carousel" (aria-roledescription) without that word in its label, and each card is a group (role="group") announced as "slide". At either end, the button that can't move the rail stays visible and focusable, with aria-disabled="true".
The demo: a carousel
Nothing starts on its own. Under reduced motion dragging still works, but there is no momentum: on release a 0.2 s tween snaps to the nearest card, and the buttons move without animating, through gsap.matchMedia(), as covered in post 2.
Common mistakes
inertia: truewithout InertiaPlugin loaded. On release there is no momentum and no snapping, and Draggable 3.15.0 logs nothing. IfregisterPluginnames the missing plugin, that line throws aReferenceError.- Reading
pageXfrom the event. The right values arethis.pointerXandthis.pointerY, which GSAP normalizes across browsers. - Moving the element with a tween and skipping
update(). Draggable keeps its oldxandy, and the next click on "Next" goes nowhere. - Timelines and Draggable on the same element. Per the docs, a proxy element may be needed to take the drag.
- Dragging as the only way to use the control. It fails WCAG 2.5.7 and 2.1.1; buttons are needed.
FAQ
Does Draggable work with a finger?
Yes, with mouse and touch. With simulated touch, a 180 px flick in the demo slows down and stops right on a card.
Snap without Inertia?
Yes: liveSnap snaps during the drag, and on release a tween in onDragEnd can do the snapping, as the demo does under reduced motion. The release snap needs InertiaPlugin.
Conclusion
The rest of the series: post 3, post 4, post 6 and post 7. Next up is post 9, on Observer: wheel, touch and pointer gestures without dragging an element; in post 13, the final tutorial, the carousel runs on Draggable and Inertia.
References
- GSAP — Draggable
- GSAP — InertiaPlugin
- GSAP — GSAP 3.13
- W3C — WCAG 2.2
- W3C — Understanding 2.5.7: Dragging Movements
- W3C — ARIA APG: Carousel
Tags
Share this post
Subscribe
Get the latest posts delivered right to your inbox.
Leave a comment