# 下拉刷新

向下拉动印刷记录列表，加载一条最新到件。

## 试一试

从顶部下拉，看到刷新提示后松手，或点击刷新按钮。

## 交互细节

向下拖动 82 像素后松手即可刷新。提前松手或取消会让列表复位。刷新期间忽略重复输入，每次添加三条示例记录中的一条。启用减弱动态效果时，用状态文字替代旋转动画。

## 各平台的操作方式

- iOS · 从顶部箭头向下拉，松手即可刷新。也可以轻点刷新按钮。
- Android · 从顶部箭头向下拉，出现“松开即可刷新”后松手。
- PC · 将列表顶部向下拖动，或用键盘聚焦刷新按钮并按回车。

## 适用场景

收件日志、动态消息或按时间更新的列表。

## 实现要求

> 达到距离阈值后松手再刷新，提前释放则复位；同时提供按钮和文字状态。

## 来源与致谢

[Android Developers · Pull to refresh](<https://developer.android.com/develop/ui/compose/components/pull-to-refresh>)

## 独立 HTML 示例

将以下代码保存为 HTML 文件，用浏览器打开即可离线体验。

```html
<!doctype html>
<html lang="zh-Hans"><head><script>if(new URLSearchParams(location.search).get('reduce')==='1')document.documentElement.classList.add('motion-reduced');</script><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>下拉刷新 · Motion Lab</title>
<style>
*{box-sizing:border-box}html{color-scheme:light}body{margin:0;font:15px/1.5 system-ui,-apple-system,'Malgun Gothic',sans-serif;word-break:keep-all}main{margin:auto}button{font:inherit;color:inherit;cursor:pointer}button:focus-visible,[tabindex]:focus-visible{outline:3px solid #005bc6;outline-offset:2px}button:disabled{cursor:default}h1,h2,p{margin:0}h1{font-size:clamp(26px,5vw,42px);font-weight:750;letter-spacing:-.075em;line-height:1.2}h2{font-size:18px;letter-spacing:-.04em}.mono{font:600 11px/1.4 ui-monospace,Consolas,monospace;letter-spacing:.055em}.sr{position:absolute;width:1px;height:1px;overflow:hidden;clip-path:inset(50%);white-space:nowrap}button{touch-action:manipulation}html.motion-reduced *,html.motion-reduced *::before,html.motion-reduced *::after{animation-duration:.01ms!important;animation-iteration-count:1!important;transition-duration:.01ms!important;scroll-behavior:auto!important}@media(prefers-reduced-motion:reduce){*,*::before,*::after{animation-duration:.01ms!important;animation-iteration-count:1!important;transition-duration:.01ms!important;scroll-behavior:auto!important}}

body{background:#dae4d9;color:#132d25}main{width:min(100%,660px);padding:clamp(15px,4vw,40px)}.ledger{background:#f3f3dd;border-block:2px solid #183e31;padding:clamp(15px,4vw,32px);box-shadow:0 12px 35px #153a2517}.head,.foot,.row{display:flex;align-items:center;justify-content:space-between;gap:12px}.head{align-items:end}.head h1{margin-top:5px}.seal{display:grid;place-items:center;border:2px double #27503b;color:#27503b;border-radius:50%;width:64px;height:64px;text-align:center;line-height:1.08;transform:rotate(-10deg);font:italic 700 18px/1 Georgia,serif}.pull{margin-top:18px;height:50px;touch-action:none;cursor:grab;user-select:none;display:flex;align-items:center;justify-content:center;gap:8px;background:#d7e2c5;border-block:1px dashed #708771;color:#214833}.pull:active{cursor:grabbing}.dial{font-size:24px;line-height:1}.well{overflow:hidden}.rows{list-style:none;padding:0;margin:0;transform:translateY(var(--pull,0px));transition:transform .28s cubic-bezier(.19,.8,.24,1)}.dragging .rows{transition:none}.row{position:relative;min-height:58px;justify-content:flex-start;padding:8px 0;border-bottom:1px solid #abb59e}.row time{margin-left:auto;font-size:12px}.thumb{height:36px;width:34px;flex:none;background:radial-gradient(ellipse at 47% 45%,var(--tone) 0 34%,#f9e2a4 35% 41%,transparent 42%),repeating-linear-gradient(33deg,#244c39 0 2px,#96ab79 2px 5px);border:1px solid #12352e}.row strong{display:block;font-size:15px}.row small{color:#435547;font-size:11px}.foot{margin-top:13px}.foot p{font-size:12px;color:#344e3d}.foot button{border:0;background:#244532;color:white;min-height:44px;padding:6px 16px;font-weight:700}.foot button:disabled{background:#4c5e50}.busy .dial{animation:spin .72s linear infinite}.row.new{animation:arrival .45s ease-out}@keyframes spin{to{transform:rotate(1turn)}}@keyframes arrival{from{opacity:.15;transform:translateY(-18px)}to{opacity:1;transform:none}}@media(max-width:370px){.seal{width:48px;height:48px;font-size:15px}.row{gap:8px}.foot button{padding-inline:10px}}

</style></head><body>

<main><section class="ledger" aria-labelledby="title"><header class="head"><div><span class="mono">印刷车间 · 到件记录</span><h1 id="title">今日校样</h1></div><span class="seal" aria-hidden="true">Proof<br>Desk</span></header>
<div class="pull" id="pull"><span class="dial" aria-hidden="true">↓</span><span id="hint">下拉查看新记录</span></div>
<div class="well"><ul class="rows" id="rows" aria-label="最近到件"></ul></div><footer class="foot"><p id="status" role="status">最近 3 条到件</p><button id="refresh" type="button">刷新</button></footer></section></main>

<script>

const rows=document.getElementById('rows'),pull=document.getElementById('pull'),hint=document.getElementById('hint'),status=document.getElementById('status'),refresh=document.getElementById('refresh'),ledger=document.querySelector('.ledger');
const records=[['夏日装订','封面校样','09:12','#ab5030'],['波浪间隔','正文校样','09:45','#356258'],['公园字样','专色检查','10:20','#2c4f48'],['青绿版画工作室','孔版海报','10:32','#d35c37'],['窗边样本','专色校样','10:47','#45699d'],['步行坐标','折页校样','11:08','#8f4151']];
let total=3,gesture=null,busy=false;
function display(fresh){rows.replaceChildren(...records.slice(0,total).reverse().slice(0,3).map(([name,detail,time,tone],i)=>{const li=document.createElement('li');li.className='row'+(fresh&&i===0?' new':'');li.innerHTML='<i aria-hidden="true" class="thumb"></i><div><strong></strong><small></small></div><time class="mono"></time>';li.style.setProperty('--tone',tone);li.querySelector('strong').textContent=name;li.querySelector('small').textContent=detail;li.querySelector('time').textContent=time;return li;}));}
function rest(){ledger.classList.remove('dragging');rows.style.removeProperty('--pull');pull.querySelector('.dial').style.removeProperty('transform');hint.textContent='下拉查看新记录';}
function load(){if(busy)return;rest();gesture=null;busy=true;ledger.classList.add('busy');refresh.disabled=true;rows.setAttribute('aria-busy','true');pull.querySelector('.dial').textContent='↻';hint.textContent='正在检查记录';status.textContent='正在查看车间到件日志。';
setTimeout(()=>{const fresh=total<records.length;if(fresh)total++;display(fresh);busy=false;refresh.disabled=false;ledger.classList.remove('busy');rows.setAttribute('aria-busy','false');rest();pull.querySelector('.dial').textContent='↓';status.textContent=fresh?records[total-1][0]+' 已收到 · 显示最近三条':'当前已是最新记录。';},450);}
pull.addEventListener('pointerdown',e=>{if(!e.isPrimary||e.button!==0||busy)return;gesture={id:e.pointerId,start:e.clientY,dy:0};pull.setPointerCapture(e.pointerId);ledger.classList.add('dragging');});
pull.addEventListener('pointermove',e=>{if(!gesture||e.pointerId!==gesture.id)return;gesture.dy=Math.max(0,e.clientY-gesture.start);rows.style.setProperty('--pull',Math.min(44,gesture.dy*.45)+'px');pull.querySelector('.dial').style.transform='rotate('+Math.min(180,gesture.dy*2)+'deg)';hint.textContent=gesture.dy>=82?'松开即可刷新':'再往下拉一点';});
function finish(e){if(!gesture||e.pointerId!==gesture.id)return;const commit=e.type==='pointerup'&&gesture.dy>=82;gesture=null;rest();if(commit)load();}
['pointerup','pointercancel','lostpointercapture'].forEach(type=>pull.addEventListener(type,finish));
document.addEventListener('keydown',e=>{if(e.key==='Escape'&&gesture){gesture=null;rest();}});refresh.addEventListener('click',load);display(false);

</script></body></html>
```
