Gesture Pull-to-Refresh
手势下拉刷新
Pulling the list reveals a threshold indicator that becomes a refresh operation on release.
Where to use it
Refresh a timeline without replacing the page or moving its primary controls. Arm refresh only when the list is already at the top and give a clear visual cue when release will trigger it.
Practical example
In a transit arrivals list, pulling down at the top gradually fills the refresh indicator. After it reaches the threshold, releasing checks for new departure times while the current list remains visible.
AI prompt
Add pull-to-refresh to [feed]. Start only at the top and during a downward drag, apply increasing resistance, arm at a fixed threshold, and also expose a visible refresh button.
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>Gesture Pull-to-Refresh</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; }
}
.pull-stage{position:relative;overflow:hidden;height:103px;border-radius:10px;background:#252341}.pull-strip{position:absolute;top:2px;left:50%;translate:-50% 0;display:grid;place-items:center;width:30px;height:30px;border:1px solid #a7d8d5;border-radius:50%;color:#d0eeec}.pull-stage[data-arm] .pull-strip{background:#b8ebe0;color:#23324b}.pull-feed{position:relative;display:grid;gap:5px;touch-action:none;transform:translateY(var(--pull,0px));transition:transform .22s ease;cursor:ns-resize}.pull-feed[data-drag]{transition:none}
</style>
</head>
<body>
<main class="demo">
<div class="ui"><div class="pull-stage"><div class="pull-strip" aria-hidden="true">↓</div><div class="pull-feed"><div class="surface bar"><strong>Field log</strong><span class="mono revision">#01</span></div><div class="surface muted">Drag this list down</div></div></div><div class="bar"><span class="muted" role="status">Pull to refresh</span><button class="button refresh" type="button">Refresh</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 feed = document.querySelector('.pull-feed');
const stage = feed.parentElement;
const status = document.querySelector('[role=status]');
const control = document.querySelector('.refresh');
const strip = document.querySelector('.pull-strip');
let busy = false, revision = 1;
const draw = (amount) => feed.style.setProperty('--pull', amount + 'px');
function refresh() {
if (busy) return;
busy = true; control.disabled = true; draw(33); strip.textContent = '↻';
status.textContent = "Refreshing…";
setTimeout(() => { revision += 1; feed.querySelector('.revision').textContent = '#' + String(revision).padStart(2, '0'); draw(0); strip.textContent = '↓'; stage.removeAttribute('data-arm'); status.textContent = "Feed updated"; busy = false; control.disabled = false; }, 680);
}
dragOn(feed, { start() { if (!busy) feed.setAttribute('data-drag', ''); }, move(delta) { if (!busy) { const armed = delta.y >= 54; draw(clamp(delta.y * .63, 0, 43)); stage.toggleAttribute('data-arm', armed); status.textContent = armed ? "Release to refresh" : "Pull to refresh"; } }, end(delta, canceled) { feed.removeAttribute('data-drag'); if (busy) return; if (!canceled && delta.y >= 54) refresh(); else { draw(0); stage.removeAttribute('data-arm'); } } });
control.addEventListener('click', refresh);
</script>
</body>
</html>Original example by UI Skills