Swipe to Confirm
滑动确认
A thumb follows the gesture and completes only at the far end of the track.
Where to use it
Make a commitment visually deliberate in a mobile workflow. Mark the destination visibly and commit the action only when the thumb is released in its finished position.
Practical example
At the end of a stockroom count, a worker slides “Submit count” to the far end of the rail and releases to send it. Letting go halfway snaps the thumb back and leaves the count open.
AI prompt
Build swipe confirmation for [action]. Clamp travel to the visible rail, commit only on release near the end, return to zero if canceled, and expose a focusable keyboard equivalent.
Example code
Save the standalone HTML as a file and open it in your browser. It uses the same code as the preview.
View HTML
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Swipe to Confirm</title>
<style>
:root {
color-scheme: dark;
--demo-bg: #171629;
--demo-ink: #f8f5ff;
--demo-accent: #b9a6ff;
--demo-muted: #bfbbd3;
font-family: system-ui, sans-serif;
color: var(--demo-ink);
background: var(--demo-bg);
}
*, *::before, *::after { box-sizing: border-box; }
body { margin: 0; min-width: 0; }
button, input { font: inherit; }
.demo {
display: grid;
place-items: center;
align-content: center;
min-height: 100vh;
padding: 14px;
overflow: hidden;
background: radial-gradient(ellipse at 50% 24%, #28233f, var(--demo-bg) 78%);
}
.demo-button {
border: 1px solid #ffffff3d;
border-radius: 7px;
background: #ffffff12;
color: var(--demo-ink);
padding: 6px 11px;
font-size: 12px;
cursor: pointer;
}
:focus-visible {
outline: 2px solid var(--demo-accent);
outline-offset: 3px;
}
.ui { width: min(100%, 238px); display: grid; gap: 8px; position: relative; font-size: 12px; line-height: 1.35; }
.ui .surface { min-width: 0; border: 1px solid #676591; border-radius: 10px; background: #282744; padding: 8px; }
.ui .bar { display: flex; align-items: center; justify-content: space-between; gap: 6px; }
.ui .actions { display: flex; align-items: center; justify-content: center; flex-wrap: wrap; gap: 5px; }
.ui .muted { color: #cecae9; font-size: 11px; }
.ui .center { text-align: center; }
.ui .mono { font: 11px ui-monospace, monospace; font-variant-numeric: tabular-nums; }
.ui p { margin: 0; }
.ui .button { min-height: 30px; padding: 4px 7px; border-radius: 7px; border: 1px solid #79779f; background: #3d3a60; color: #f9f5ff; font-size: 11px; font-weight: 650; cursor: pointer; }
.ui .button:hover { background: #4e4976; }
.ui .primary { background: #c5eeec; border-color: #dafaee; color: #17213b; }
.ui .primary:hover { background: #e3fff9; }
.ui .button:disabled { opacity: .6; cursor: default; }
.ui .field { min-width: 0; width: 100%; border: 1px solid #79779f; border-radius: 6px; background: #1b1a31; color: #faf5ff; padding: 5px 7px; font-size: 12px; }
.ui .field::placeholder { color: #c2bcdd; }
.ui input[type="range"] { accent-color: #b8ebe0; width: 100%; min-width: 0; margin: 0; }
.ui button { font: inherit; }
.ui [hidden] { display: none !important; }
.ui progress { accent-color: #b8ebe0; }
@media (prefers-reduced-motion: reduce) {
.ui *, .ui *::after, .ui *::before { transition-duration: 0s !important; animation-duration: 0s !important; scroll-behavior: auto !important; }
}
.swipe-rail{height:44px;position:relative;border:1px solid #7876a4;border-radius:24px;background:#24223f;overflow:hidden;display:grid;place-items:center}.thumb{position:absolute;top:4px;left:4px;width:34px;height:34px;border:0;border-radius:20px;background:#b8ebe0;color:#251f3c;touch-action:none;cursor:ew-resize;transform:translateX(var(--travel,0px));transition:transform .23s ease}.thumb[data-drag]{transition:none}.swipe-rail[data-done]{background:#31554e}
</style>
</head>
<body>
<main class="demo">
<div class="ui"><div class="swipe-rail"><span class="track-label">Slide to confirm</span><button type="button" class="thumb" aria-label="Swipe or press End to confirm">→</button></div><div class="bar"><span class="muted" role="status">Ready</span><button class="button reset" type="button">Reset</button></div></div>
</main>
<script>
function dragOn(surface, handlers) {
let pointer = null;
let origin = null;
function finish(event, canceled) {
if (event.pointerId !== pointer) return;
const delta = { x: event.clientX - origin.x, y: event.clientY - origin.y };
pointer = null;
handlers.end(delta, canceled);
}
surface.addEventListener('pointerdown', (event) => {
if (pointer !== null || event.button !== 0) return;
pointer = event.pointerId;
origin = { x: event.clientX, y: event.clientY };
surface.setPointerCapture(pointer);
handlers.start?.(event);
});
surface.addEventListener('pointermove', (event) => {
if (event.pointerId === pointer) {
handlers.move({ x: event.clientX - origin.x, y: event.clientY - origin.y }, event);
}
});
surface.addEventListener('pointerup', (event) => finish(event, false));
surface.addEventListener('pointercancel', (event) => finish(event, true));
surface.addEventListener('lostpointercapture', (event) => finish(event, true));
}
const clamp = (value, low, high) => Math.min(high, Math.max(low, value));
const rail = document.querySelector('.swipe-rail');
const thumb = rail.querySelector('.thumb');
const status = document.querySelector('[role=status]');
const max = () => rail.clientWidth - thumb.offsetWidth - 8;
let done = false;
function draw(distance) { rail.style.setProperty('--travel', clamp(distance, 0, max()) + 'px'); }
function set(next) { done = next; rail.toggleAttribute('data-done', done); draw(done ? max() : 0); thumb.textContent = done ? '✓' : '→'; status.textContent = done ? "Confirmed" : "Ready"; }
dragOn(thumb, { start() { if (!done) thumb.setAttribute('data-drag', ''); }, move(delta) { if (!done) draw(delta.x); }, end(delta, canceled) { thumb.removeAttribute('data-drag'); if (!done) set(!canceled && delta.x >= max() * .83); } });
thumb.addEventListener('keydown', (event) => { if (['End', 'Enter', ' '].includes(event.key)) { event.preventDefault(); set(true); } });
thumb.addEventListener('click', (event) => { if (event.detail === 0) set(true); });
document.querySelector('.reset').addEventListener('click', () => { set(false); thumb.focus(); });
new ResizeObserver(() => { if (done) draw(max()); }).observe(rail);
</script>
</body>
</html>Original example by UI Skills