全部效果加载与反馈

长按确认

Hold to Confirm

持续按住时逐渐填满进度,达到时长才确认,提前松手则复位。

按住按钮约一秒,也可聚焦后按住空格键

适用场景

在编辑器或控制面板中区分有意操作和误触。 用填充进度告诉用户还需按多久,提前松手就取消本次确认。

应用示例

在线白板演示中,「开启限时投票」需要长按。按住时填充逐渐前进,提前松开会清空进度,投票仍处于待开始状态。

AI 提示词

给【操作】加入长按确认。用单一捕获指针或持续按住空格记录时长,达阈值只执行一次,提前松手、取消或失焦时复位,并播报结果。

示例代码

保存为 HTML 文件后,用浏览器打开即可运行。代码与页面里的演示一致。

查看 HTML
<!doctype html>
<html lang="zh">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>长按确认</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; }
    }
    .ui .hold{position:relative;isolation:isolate;overflow:hidden;min-height:46px;background:#342b58;touch-action:none;user-select:none}.hold::before{content:'';position:absolute;z-index:-1;inset:0;background:#79c4b169;transform:scaleX(var(--fill,0));transform-origin:left center}.hold[data-state=confirmed]{color:#e1fff2;border-color:#83efbc}.hold span{font-weight:650}
  </style>
</head>
<body>
  <main class="demo">
    <div class="ui"><button class="button hold" data-testid="hold-confirm-action" type="button" data-state="idle"><span>按住确认</span></button><div class="bar"><span class="muted" role="status" data-testid="hold-confirm-status">等待操作</span><button class="button reset" type="button">重置</button></div></div>
  </main>
  <script>
    const action = document.querySelector('.hold');
    const status = document.querySelector('[role=status]');
    const label = action.firstElementChild;
    const labels = {"idle":"按住确认","waiting":"等待操作","holding":"继续按住","canceled":"提前松手","confirmed":"已确认"};
    let frame = 0;
    let input = null;
    let startedAt = 0;
    function fill(amount) { action.style.setProperty('--fill', amount); }
    function step(now) {
      if (input === null) return;
      const progress = Math.min(1, (now - startedAt) / 900);
      fill(progress);
      if (progress < 1) { frame = requestAnimationFrame(step); return; }
      input = null; action.dataset.state = 'confirmed'; status.textContent = labels.confirmed; status.setAttribute('data-confirmed', ''); label.textContent = labels.confirmed;
    }
    function begin(source) {
      if (input !== null || action.dataset.state === 'confirmed') return;
      input = source; startedAt = performance.now(); action.dataset.state = 'holding'; status.textContent = labels.holding; frame = requestAnimationFrame(step);
    }
    function release(source) {
      if (input !== source || input === null) return;
      input = null; cancelAnimationFrame(frame); fill(0); action.dataset.state = 'idle'; status.textContent = labels.canceled;
    }
    action.addEventListener('pointerdown', (event) => { if (event.button === 0 && input === null) { action.setPointerCapture(event.pointerId); begin(event.pointerId); } });
    for (const name of ['pointerup', 'pointercancel', 'lostpointercapture']) action.addEventListener(name, (event) => release(event.pointerId));
    for (const name of ['keydown', 'keyup']) action.addEventListener(name, (event) => {
      if (event.key !== ' ' && event.key !== 'Enter') return;
      event.preventDefault();
      if (name === 'keydown') { if (!event.repeat) begin(event.key); }
      else release(event.key);
    });
    action.addEventListener('blur', () => release(input));
    document.querySelector('.reset').addEventListener('click', () => { release(input); fill(0); action.dataset.state = 'idle'; status.textContent = labels.waiting; status.removeAttribute('data-confirmed'); label.textContent = labels.idle; action.focus(); });
  </script>
</body>
</html>

UI Skills 原创示例

相关参考